help about drawing

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
drmacro
Veteran
Posts: 8865
Joined: Sun Mar 02, 2014 4:35 pm

Re: help about drawing

Post by drmacro »

heda wrote: Tue Oct 26, 2021 3:36 pm ...

can be done with the one-liner

Code: Select all

pts = [makePt(x, y) for x, y in zip(xpts, ypts)]
...
The pythonistas or language lawyers would probably consider this "more Pythonic". Why? Well it's more obfuscated, therefore it must be better. :mrgreen: ;)
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: help about drawing

Post by heda »

:lol: had it coming (of course) :D
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: help about drawing

Post by TheMarkster »

heda wrote: Tue Oct 26, 2021 3:36 pm off-topic.
any code that works is good code in my book, however cannot resist from putting a slight variation of marksters code out there,
does the same - is just a bit more compact...

the passage

Code: Select all

pts = []
ii=0
for x in xpts:
    y = ypts[ii]
    pts.append(makePt(x,y))
    ii += 1
can be done with the one-liner

Code: Select all

pts = [makePt(x, y) for x, y in zip(xpts, ypts)]
or if one prefers loops

Code: Select all

pts = list()
for x, y in zip(xpts, ypts):
    pts.append(makePt(x, y))
my personal preference is the one-liner, easy to both read and write once one is used to that type of construct, and who knows, might be the fastest one (on the margin) as well :-)
Cool. Thanks for not resisting. You have taught me something new -- a way to use zip() in list comprehension. We can, of course, refine this even further by eliminating the function and the pts list:

Code: Select all

import Draft
xpts = [0,0,0,0.01,0.02,.05,0.11,0.26,0.55,1.06,1.81,2.78,3.95,5.28,6.72,8.22,9.74,11.27,12.80,14.33]
ypts = [18.98,17.45,15.91,14.38,12.85,11.32,9.80,8.27,6.77,5.33,4.00,2.82,1.84,1.08,0.57,0.26,0.11,0.04,0.01,0.0]
doc = FreeCAD.newDocument() if not FreeCAD.ActiveDocument else FreeCAD.ActiveDocument
Draft.makeBSpline([FreeCAD.Vector(x,y,0) for x,y in zip(xpts,ypts)])
doc.recompute()
Edit: corrected bug in doc = line
Post Reply