Click here to Skip to main content
16,018,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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 b1 = this.CalculateB(yPosition, m1, xPosition);
// double angleHeight = angle; //Math.Atan(m1);

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};


C#
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);
}
Posted
Comments
sreeyush sudhakaran 21-Oct-15 7:35am    
Better to ask in some Maths forum I think :) , If possible can you please share the reference link for this algorithm
A helpful Link : http://www.mathopenref.com/coordpolygonarea.html
http://www.mathopenref.com/indexpage.html (See Triangle Here)
Fkutsche 21-Oct-15 8:30am    
Thanks for your comment! I asked in a Math forum and I'm waiting for a reply there at the moment. I will share my solution here when it's working :)
Ralf Meier 21-Oct-15 8:30am    
I don't understand your explaination exactly.
Could you give one or two examples which shows what you explained, what happens and what you want to happen ?

In your code I don't see when you take the orthocentre-coordinates. It should be only at the MouseClick-Moment and not during the whole MouseDown-time ...
Fkutsche 21-Oct-15 8:37am    
The orthocentre-coordinates are taken at the Mouse-Click moment, so theys stay the same for the whole process.

Here is an example how it should look:
http://www.mathelounge.de/?qa=blob&qa_blobid=9135849813851033220

Here is an example how it should NOT look:
http://www.mathelounge.de/?qa=blob&qa_blobid=4668010827737964515

Here's my german explanation, I've seen you're german, so maybe I explained it better there.
http://www.mathelounge.de/275282/punkte-gleichschenkligen-dreiecks-einem-punkt-allen-winkeln
Ralf Meier 21-Oct-15 8:57am    
Thanks for the Links - but I think we should remain here because I'm not sure where to answer ...

When looking to your Screenshots : where are the Coordinates saved in the Moment of MouseClick ? In the Center of the circle ? Somewhere on the line from the triangle to the center of the control ?
To which point should they correspond ? to the Center of your Control ?
Is it a kind of AngleEditor you want to create ? Or a Kompass ?

1 solution

I have it!

This method is all i need for it basically. I found a completely different algorithm for it.

C#
private PointCollection CalculatePoints(Point cPoint, Point pPoint, double gamma, double rad)
        {
            double mPC = this.CalculateMWithTwoPoints(cPoint.X, cPoint.Y, pPoint.X, pPoint.Y);
            double mN = -(1/mPC);
            double PA = Math.Tan(gamma)*rad;
            double alpha = Math.Atan(mN);
            double deltaY = Math.Sin(alpha) * PA;
            double deltaX = Math.Cos(alpha) * PA;
            return new PointCollection() { new Point(pPoint.X - deltaX, pPoint.Y - deltaY), new Point(pPoint.X + deltaX, pPoint.Y + deltaY), cPoint };
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900