Skip to content

Instantly share code, notes, and snippets.

@ryanbugden
Last active August 21, 2020 17:12
Show Gist options
  • Save ryanbugden/447f8260396ac0292a704eaf01056263 to your computer and use it in GitHub Desktop.
Save ryanbugden/447f8260396ac0292a704eaf01056263 to your computer and use it in GitHub Desktop.
Select four points. Make their line segments parallel by running.
# menuTitle : Make Two Selected Lines Parallel
import math
def sortTuple(tup):
# sort line coordinates in ascending y order
tup.sort(key = lambda x: x[1])
return tup
g = CurrentGlyph()
line1_pts = []
line2_pts = []
i = 1
for pt in g.selectedPoints:
if i < 3:
line1_pts.append((pt.x, pt.y))
elif 2 < i < 5:
line2_pts.append((pt.x, pt.y))
i +=1
# sort list by y's
line1_pts = sortTuple(line1_pts)
line2_pts = sortTuple(line2_pts)
print("line 1: ", line1_pts)
print("line 2: ", line2_pts)
x1, y1, x2, y2 = line1_pts[1][0]-line1_pts[0][0], line1_pts[1][1]-line1_pts[0][1], line2_pts[1][0]-line2_pts[0][0], line2_pts[1][1]-line2_pts[0][1]
line1_ang, line2_ang = math.degrees(math.atan2(y1, x1)), math.degrees(math.atan2(y2, x2))
avg_ang = (line1_ang + line2_ang)/2
print("line 1 angle: ", line1_ang)
print("line 2 angle: ", line2_ang)
print("average/target angle: ", avg_ang)
print("-----")
print("x1: ", x1)
print("y1: ", y1)
print("x2: ", x2)
print("y2: ", y2)
# desired x for top point for line 1
line1_xdiff = x1 - (y1 / math.tan(math.radians(avg_ang)))
# desired x for top point for line 2
line2_xdiff = x2 - (y2 / math.tan(math.radians(avg_ang)))
# factor by which the top shares the xdiff (1/2 for now? should it be split proportionately?)
factor_top = 0.5
# factor by which the bottom shares the xdiff (1/2 for now? should it be split proportionately?)
factor_bot = 0.5
# move points
with g.undo("Make lines parallel"):
i = 1
for pt in g.selectedPoints:
if (pt.x, pt.y) == line1_pts[1]:
pt.x -= line1_xdiff * factor_top
elif (pt.x, pt.y) == line2_pts[1]:
pt.x -= line2_xdiff * factor_top
elif (pt.x, pt.y) == line1_pts[0]:
pt.x += line1_xdiff * factor_bot
elif (pt.x, pt.y) == line2_pts[1]:
pt.x += line2_xdiff * factor_bot
g.changed()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment