创建一个动态三角形 [英] Create a dynamic Triangle

查看:88
本文介绍了创建一个动态三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我想在WPF中创建一个动态三角形。我的代码几乎正常工作,但我遇到了一个小问题。我有以下场景:用户点击我的GUI,现在他可以在按下左键的同时移动鼠标。当他移动它时,它会创建一个三角形。三角形的正中心(应该)始终是第一次按下鼠标左键的位置。高度点始终是光标所在的位置。这一点的角度(在这种情况下)总是40°。



问题是,正中心在那个位置应该是准确的在我的圈子中的角度时间。



我认为我的算法有点错误,但我无法想象。也许你们可以给我一些意见。



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







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

推荐答案

我拥有它!



这个方法基本上都是我需要的。我找到了一个完全不同的算法。



I have it!

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

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


这篇关于创建一个动态三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆