ios - How to draw a straight perpendicular line between 2 other parallel lines? -
enter image description here need draw first line using location of finger. later need draw second parallel line using location of finger. have done it. , main task draw third perpendicular line between parallel lines. how can draw third line?
if have 2 parallel lines , wish draw perpendicular line between them need 1 point. assume point @ middle of first line (call c).
also assume have following:
l1 // represents first line l2 // represents second line l1.start // represents line start point cgpoint l1.end // represents line end point cgpoint now wish draw perpendicular line first line l1 , need normal pretty simple in 2d. first line direction subtracting start , end point of given line direction = cgpoint(x: l1.end.x-l1.start.x, y: l1.end.y-l1.start.y). normal need invert coordinates , divide them direction length:
let length = sqrt(direction.x*direction.x + direction.y*direction.y) let normal = cgpoint(x: -direction.y/length, y: direction.x/length) // yes, normal (-d.y, d.x) so said start point c need find end point on other line c + normal*distancebetweenlines. need distance between 2 lines should best received through dot product...
first need vector between pair of points 2 lines (one point on first line , other 1 on second line). let's take
let between = cgpoint(x: l1.start.x-l2.start.x, y: l1.start.y-l2.start.y)
now need project line normal dot product length of projection length between 2 lines
let distancebetweenlines = between.x*normal.x + between.y*normal.y.
so have points draw perpendicular line between 2 given lines assuming lines parallel:
l3.start = c l3.end = cgpoint(x: c.x + normal.x*distancebetweenlines, y: c.y + normal.y*distancebetweenlines)
Comments
Post a Comment