Calculate third point of equilateral triangle

Given two point how do you calculate the third point which will make an equilateral triangle. This is the code i have.

publicclass Equilateral

{

publicstaticvoid main(String[]args)

{

EasyReader in =new EasyReader();

System.out.print("x1 : ");

double x1 = in.readDouble();

System.out.print("y1 : ");

double y1 = in.readDouble();

System.out.print("x2 : ");

double x2 = in.readDouble();

System.out.print("y2 : ");

double y2 = in.readDouble();

double xs = Math.pow((x2-x1),2);

double ys = Math.pow((y2-y1),2);

double d1 = Math.sqrt(xs+ys);////distance of base

double midpointX = (x1+x2)/2;

double midpointY = (y1+y2)/2;///midpoint of base

double DHBX = Math.pow((x2-midpointX),2);

double DHBY = Math.pow((x2-midpointY),2);

double DHB = Math.sqrt(DHBX+DHBY);////distance of half base

double height = DHB*(Math.sqrt(3));

}

}

[1905 byte] By [FrancoNa] at [2007-10-3 5:26:13]
# 1

> Given two point how do you calculate the third point

> which will make an equilateral triangle. This is the

> code i have.

That isn't a Java related question is it? That's a geometry question, and I'm sure it's part of your homework to figure out, rather than ask for someone to just do it.

But just wait patiently for a bit, and the homework slave known as Norweed will do it for you.

warnerjaa at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 2
Read through [url http://www.mathpages.com/home/kmath270/kmath270.htm]this[/url] page.
CaptainMorgan08a at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 3
> But just wait patiently for a bit, and the homework> slave known as Norweed will do it for you.lol
manuel.leiriaa at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 4
You should note that there are two solution for each pair of points so I would expect your teacher to want both solutions.
sabre150a at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 5

ok so this is my code now and it doesn't seem to print out the correct points

public class Equilateral

{

double x1, x2, x3, x4, y1, y2, y3, y4, xs, ys, d1, midpointX, midpointY, DHBX, DHBY, DHB, height, perpenSlope, slopeBase, b, A, B, C;

public Equilateral()

{

EasyReader in = new EasyReader();

System.out.print("x1 : ");

x1 = in.readDouble();

System.out.print("y1 : ");

y1 = in.readDouble();

System.out.print("x2 : ");

x2 = in.readDouble();

System.out.print("y2 : ");

y2 = in.readDouble();

xs = Math.pow((x2-x1),2);

ys = Math.pow((y2-y1),2);

d1 = Math.sqrt(xs+ys);////distance of base

midpointX = (x1+x2)/2;

midpointY = (y1+y2)/2;///midpoint of base

DHBX = Math.pow((x2-midpointX),2);

DHBY = Math.pow((x2-midpointY),2);

DHB = Math.sqrt(DHBX+DHBY);////distance of half base

height = (DHB/2)*(Math.sqrt(3));

slopeBase = ((y1-y2)/(x1-x2));

if(slopeBase == 0)

{

x3 = midpointX;

y3 = height + y1;

}

else

{

perpenSlope = -(1/slopeBase);

b = perpenSlope*(-1*midpointX)+midpointY;

A = 1 + Math.pow(perpenSlope,2);

B = -2*midpointX + 2*perpenSlope*b - 2*perpenSlope*midpointY;

C = Math.pow(midpointX,2) + Math.pow(b,2) - 2*b*midpointY + Math.pow(midpointY,2) - Math.pow(height,2);

x3 = ((-(B))+Math.sqrt(Math.pow(B,2)-(4*A*C)))/(2*A);

y3 = perpenSlope*x3 + b;

x4 = ((-(B))-Math.sqrt(Math.pow(B,2)-(4*A*C)))/(2*A);

y4 = perpenSlope*x4 + b;

}

System.out.println("Point 1 : ("+x3+", "+y3+")");

System.out.println("Point 2 : ("+x4+", "+y4+")");

}

}

FrancoNa at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 6
> ok so this is my code now and it doesn't seem to print out the correct pointsWhat does it print out, and what should it print out?
doremifasollatidoa at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 7
Well, my teacher is asking me to put in x1: 0, y1: 0, x2: -3, y2: 4. My program prints out (2.1166282640050245, 4.712471198003769) for point one and (-5.1166282640050245, -0.712471198003769) for point two. Notice that the numbers after the decimals after the same for each x and each y.
FrancoNa at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 8
The answer should be (1.9, 4.5) (-4.9, -.5)
FrancoNa at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 9

But, are they the numbers you expect? Is the distance between points equal for all pairs of points (not between the two calculated points, of course)? Without a detailed analysis of your algorithm, the points look reasonable to me.

Edit: I see your answers now. You aren't *too* far off. But, you do see that the decimal portions are the same for both pairs of x/y.

Does using x*x instead of Math.pow(x, 2) do anything for your accuracy?

doremifasollatidoa at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 10
You have a typo in the calculation of DHBY.You have some other error somewhere, too. Not sure where.
doremifasollatidoa at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...
# 11
I suggest you don't use the tangent (slopeBase) since tangent's have a nasty habit of going infinite. Keep the delta X and delta Y separarely.
malcolmmca at 2007-7-14 23:33:27 > top of Java-index,Java Essentials,Java Programming...