Hello,
i want to create a dynamic triangle in WPF. My code is nearly working, but i have a small problem. I have the following scenario: The User clicks on my GUI, now he can move the mouse while the left button is pressed. When he moves it, it creates a triangle. The orthocentre of the triangle is (should be) always where the left mouse button was first pressed. The height point is always where the cursor is at the moment. The angle in this point is (in this case) always 40°.
The problem is, that the orthocentre is at that point where it should be just at to exact angles times in my circle.
I think something of my algorithm is a bit wrong, but i can't imagine what. Maybe you guys can give me some input.
Point aPoint = new Point();
Point bPoint = new Point();
Point cPoint = new Point(e.GetPosition(Canvas1).X, e.GetPosition(Canvas1).Y);
Point helpPoint = new Point(xPosition, yPosition);
const double cameraAngle = 20 * Math.PI / 180;
double m1 = this.CalculateMWithTwoPoints(cPoint.X, cPoint.Y, helpPoint.X, helpPoint.Y);
double m2 = 1/m1;
double b2 = this.CalculateB(helpPoint.X, m2, helpPoint.Y);
double m3 = Math.Tan(angle + cameraAngle);
double b3 = this.CalculateB(cPoint.Y, m3, cPoint.X);
double xA = this.CalculateX(m2, m3, b2, b3);
double yA = this.CalculateY(m3, xA, b3);
aPoint.X = xA;
aPoint.Y = yA;
double m4 = Math.Tan(angle - cameraAngle);
double b4 = this.CalculateB(cPoint.Y, m4, cPoint.X);
double xB= this.CalculateX(m2, m4, b2, b4);
double yB = this.CalculateY(m4, xB, b4);
bPoint.X = xB;
bPoint.Y = yB;
polygon.Points = new PointCollection { aPoint, bPoint, cPoint};
private double CalculateMWithTwoPoints(double x1, double y1, double x2, double y2)
{
return (y2 - y1) / (x2 - x1);
}
private double CalculateB(double y, double m, double x)
{
return -(m * x - y);
}
private double CalculateY(double m, double x, double b)
{
return m * x + b;
}
private double CalculateX(double m1, double m2, double b1, double b2)
{
return (b1 - b2) / (m1 + m2);
}