Holger
Holger

Reputation: 571

Draw line 90 degrees from line

On an iOs device I have drawn a line between a startPoint and a endPoint. After drawing that line I want to proceed drawing with 90 degrees from the endPoint. So I have to create a Point on the Screen which I don't know, because the line could be in any angular.

All was programmed using the CGContextAddLineToPoint.

What is the right direction to look?

Upvotes: 2

Views: 1602

Answers (2)

Tommy
Tommy

Reputation: 100632

Call the points you know A and B, and the one you don't C.

Then the vector from A to B is:

vec.x = B.x - A.x;
vec.y = B.y - A.y;

To rotate a 2d vector by 90 degrees, switch the components and negate one. So you could create:

rightVec.x = vec.y;
rightVex.y = -vec.x;

And then position C at :

C = B + t*rightVec;

For some non-zero t.

Upvotes: 1

Iridium
Iridium

Reputation: 23721

To draw a line that is perpendicular to the line startPoint -> endPoint, the line needs to start at endPoint, and go to the point defined by:

X = endPoint.X + (startPoint.Y - endPoint.Y)
Y = endPoint.Y + (endPoint.X - startPoint.X)

The line will then be the same length as the original line, but 90 degrees to it, starting at the end of the initial line.

Upvotes: 1

Related Questions