tags:

views:

489

answers:

6

Hello,

I have a triangle ABC,

I know all points' coordinates A(x1, y1), B(x2, y2), C(x3, y3)

I want to draw a vertical edge from A to [BC]

How can i calculate?

Thanks your advance

A: 

See Bresenhams line alogirthm.

jeffamaphone
A: 

First, you have to find the slope of [BC] by s = (y3-y2) / (x3 - x2). m = (-1/s) gives you the slope of normal of [BC]. By that, y = mx + b you can take find the equation of the line of vertical that you want to draw. As x and y, you must put x1 and y1 of A. After you find this equation, you can do all sort of things you want I guess.

kolistivra
and, please, CAREFULLY track the case when x3=x2 AND when y3=y2. Same issue in y=mx+b -- use kx+ny+p = 0 instead.
Pavel Shved
A: 

What you call "vertical edge" is the altitute (i.e. segment [AH], orthogonal to [BC])?

If yes then calculate BH/CH ratio. First use trigonometrical relations in the right triangles ABH and ACH (do it for acute triangle, in obtuse the relations will be the same), then use of Law of cosines. You'll have a neat formula, where this ratio is expressed only in squares of lengths of triangle's sides. Calculate square of a side is easy, since you know cartesian coordinates of the points.

You'll get BH/CH = x/y. Rewrite it as BH*y = CH*x to avoid division by zero. Write this via vectors. And do the calculation, taking, again, special care of division by zero.

Pavel Shved
That is what i need, but i really could not figure it out.. i think it should be like that.. 1) find the B or C angle (in my triangle they are same) by atan2, 2) calculate AH by AB * cosTheta 3) calculate D coordinates by sinTheta * AH, cosTheta * AHAm i right?
You see, it's all interconnected out there. Instead of calculating angles immediately via atan (and then cosine'ing them), it's better to express angles in other terms. Eventually you'll get a way simplier formula, that doesn't need anything but +, -, * and / operators to calculate the answer.
Pavel Shved
Oh my God. If B and C angles are the same, then D IS THE MIDDLE OF BC, YOU BASTARD :-(
Pavel Shved
C(0.9330127018922193, 0.75), B(0.5, 0), A(0.5, 0.5) this is my triangle a(A) = 120, a(B) = a(C) = 30... yes D is middle of BC! now, do you really think you can calculate D(x, y) by division 2
A: 

draw a line from (x1, y1) to x1, ((y3-y2) / (x3-x2)) * (x1-x2) + y2

M. Utku ALTINKAYA
Division by zero!(I'm sorry that I keep repeating it, but it's really important in computational geometry)
Pavel Shved
what about B(0, 1), C(0, 2).. (x2 - x3) = 0 ?
yes division by zero since you can not draw a vertical line that intersects a vertical line. they are parallel. By vertical I assume you mean parallel to y axis. Handle the exception or just check whether x values are the same for B and C, do not attempt to draw if they are
M. Utku ALTINKAYA
+1  A: 

I assume you want the point where A projects onto the line determined by B and C. You can look up "vector projection" to find the math details.

multiplier = (double)((x1 - x2)*(x3 - x1) + (y1 - y2)*(y3 - y2)) / 
             (double)((x3 - x1)*(x3 - x1) + (y3 - y1)*(y3 - y1)); // dot products

x = x2 + multiplier * (x3 - x2);
y = y2 + multiplier * (y3 - y2);

There is a division by zero problem if B and C are the same point. (They wouldn't determine a line in that case.) So check for that first.

UncleO
A: 

Interesting, and a bit confusing. You want to draw a vertical line from A to BC. Not a Perpendicular line, but a vertical line! Well, that's easy! Draw from (x1, 0) to e.g. (x1, 1024). That should draw your vertical line.

To keep it in the triangle, you have to make sure that x1 is between x2 and x3, otherwise, the vertical line doesn't go through BC. Next, you need to calculate the point where this line should go through BC. If x1 == x2 then draw from A to B. If x1 == x3 then draw from A to C. Otherwise, you need to calculate the line that goes through B and C and this requires some math. Basically, we need to get something like Y=>aX+b. We can calculate a by this:

a = (y2-y3)/(x2-x3)

Next, we need to calculate b, like this:

b = y2 - (a*x2)

Now we'll calculate the value for y where x equals x1:

y = a*x1 + b

Example, A=(3,2), B=(1,4), C=(5,6) Thus, a would be (4-6)/(1-5) => -2/-4 = 0.5 And b would be 4-(0.5*1) => 3.5 Next, y. And y = 0.5*3 + 3.5 = 5.

So, you need to draw a line from (3,2) to (3,5) to get a vertical line!

Just hoping this is what you wanted. Was it homework?


Whoa! Lots of answers while I wrote mine! :-)

Workshop Alex