If you want to move the line segment, you need to change the positions of the points. In your code, at the end, you are just printing the position.
Oh, I see. However, there is still a problem. As shown in the figure below, the originally longest two lines are parallel. After I perform the opening angle, do the two red lines obtained by connecting the two nodes in parallel
However, in experiments, connecting two line segments in this way after opening an angle is not parallel, but the error is very small. How can I move the distance between two parallel lines
Do I need to recalculate the position of node movement
After opening the corners, you have new nodes, of course. You need to find the nodes
you want to move and change their x
and y
attributes, or set their position
attribute to whichever NSPoint
you like.
I understand what you are saying, but I cannot determine the coordinates of the target node. I want to achieve a line segment translation that can control the translation distance. For example, if I translate 100 distances, I will have a long parallel line. Can the code guide you. How to determine the coordinates of the target node
As shown in the following figure, I want to achieve translation of the black line perpendicular to the direction of the black line to the position of the red line segment, and how to determine the positions of the two endpoints of the red line segment. And want to control the vertical distance between the red and black segments
Please forgive me for being too difficult. I am a novice and cannot implement this code. Could you please help me
I think if you want to move the nodes programmatically, then opening the corners is not helpful. Best to move the nodes directly.
Open corners are helpful when editing the segments manually.
@mrsjing does this work as you expected?
import math
tolerance = 1 # define the maximum angle that the segments can deviate by (useful for non-orthogonal segments)
offset = 50
path = Layer.paths[0]
def angle_between_points(point1, point2):
dx = point2.x - point1.x
dy = point2.y - point1.y
return math.degrees(math.atan2(dy, dx))
def move_point(point, angle, offset):
dx = offset * math.cos(math.radians(angle))
dy = offset * math.sin(math.radians(angle))
point.x += dx
point.y += dy
segments_by_length = sorted(path.segments, key=lambda segment: segment.length()) # sorts the segments of the path into a list by their length
segment1_angle = angle_between_points(segments_by_length[-1][0], segments_by_length[-1][1])
segment2_angle = angle_between_points(segments_by_length[-2][0], segments_by_length[-2][1])
angle_diff = abs(segment1_angle - segment2_angle) # calculates the deviation in angle between the segments
if angle_diff < tolerance or abs(180-angle_diff) < tolerance:
for node in segments_by_length[-1].objects(): # objects() returns the two nodes that consitute the start and end of the segment
move_point(node, angle=segment1_angle+90, offset = -offset)
for node in segments_by_length[-2].objects():
move_point(node, angle=segment1_angle+90, offset = offset)
Thank you, it’s very helpful. However, there is a problem. When parallel lines are translated, the angle of adjacent lines changes. Can we keep the angle of adjacent line segments unchanged
The teacher SCarewe said that when an open angle can keep parallel lines moving, it can keep the angle of adjacent line segments unchanged. Use this to adjust.
Right, so you did want to keep that angle. In that case, all you have left to do is find the 4 intersections between initial adjacent lines and new parallel lines. And use that as the new node coordinates. Look up (or ask chatgpt) how to find intersection between two lines. I think that’s easier than dealing with open corners, but maybe I’m wrong.