Drawing a perpendicular line to an other line, from a point ?

Hi there,

I’ve got a problem, mainly geometrical…

I’ve drawn a segment line, from A to B. I would like to draw a third point, C, so that it draws a perpendicular line from A.

It should look like that :

schema.jpg

Do you have any clue on how to do this in lua ? Thank you in advance !

Do you already know the x,y position of point C?

Or are you asking how you would work out point C from points A and B (I presume this is the actual question)?

Nope, I don’t know the position of C. The only thing I know about C is that it’s supposed to be on the line which is perpendiculary to AB…

For now, I’ve finally done something like that :

C.x = A.x - (B.x - A.x)

C.y = A.y + (B.y - A.y)

Which actually draws CA perpendiculary to AB, but I would like to be able to set the distance between C and A…

You do know how long you want AC to be and how long AB is right? You will need to use some trigonometry. Formulas that apply can be found at : http://www.rit.edu/~w-asc/documents/services/resources/handouts/14Right%20Angle%20Trigonometry.pdf

You want to scale the AB segment down to a length of 1 first, then scale that segment by your preferred length.

The Pythagorean theorem will give you the length of AB :

local length = math.sqrt((B.x - A.x)^2 + (B.y - A.y)^2)

So divide both of  AB’s components by length, then apply the scale factor to them. Finally, plug them back in as you did before. All together, something like (untested):

local dx = B.x - A.x local dy = B.y - A.y local length = math.sqrt(dx \* dx + dy \* dy) local k = scale / length C.x = A.x - dy \* k C.y = A.y + dx \* k

Note also the swap of dx and dy. If you’re familiar with the slopes of perpendicular lines being inverse reciprocals (i.e. m and -1 / m), this is just that same thing showing up in another form (sometimes called the “perp operator”).

Both of you : thank you for your answers :wink: It helped a lot !

@ksan : Yep, I’ve already used a lot of trigonometry. But it’s been a long time since I actually learned it so I will keep the link :wink:

And StarCrunsh : this is actually what I did, juste before I had the opportunity to read your post. But it still helps to know that this was the good way to do it :wink:

So again, thank you !

Do you already know the x,y position of point C?

Or are you asking how you would work out point C from points A and B (I presume this is the actual question)?

Nope, I don’t know the position of C. The only thing I know about C is that it’s supposed to be on the line which is perpendiculary to AB…

For now, I’ve finally done something like that :

C.x = A.x - (B.x - A.x)

C.y = A.y + (B.y - A.y)

Which actually draws CA perpendiculary to AB, but I would like to be able to set the distance between C and A…

You do know how long you want AC to be and how long AB is right? You will need to use some trigonometry. Formulas that apply can be found at : http://www.rit.edu/~w-asc/documents/services/resources/handouts/14Right%20Angle%20Trigonometry.pdf

You want to scale the AB segment down to a length of 1 first, then scale that segment by your preferred length.

The Pythagorean theorem will give you the length of AB :

local length = math.sqrt((B.x - A.x)^2 + (B.y - A.y)^2)

So divide both of  AB’s components by length, then apply the scale factor to them. Finally, plug them back in as you did before. All together, something like (untested):

local dx = B.x - A.x local dy = B.y - A.y local length = math.sqrt(dx \* dx + dy \* dy) local k = scale / length C.x = A.x - dy \* k C.y = A.y + dx \* k

Note also the swap of dx and dy. If you’re familiar with the slopes of perpendicular lines being inverse reciprocals (i.e. m and -1 / m), this is just that same thing showing up in another form (sometimes called the “perp operator”).

Both of you : thank you for your answers :wink: It helped a lot !

@ksan : Yep, I’ve already used a lot of trigonometry. But it’s been a long time since I actually learned it so I will keep the link :wink:

And StarCrunsh : this is actually what I did, juste before I had the opportunity to read your post. But it still helps to know that this was the good way to do it :wink:

So again, thank you !