从圆圈中挤出线条

时间:2014-11-11 16:48:56

标签: c# wpf

我正在尝试从具有可控高度的线条的圆圈中挤出线条,如this

(抱歉我的油漆技巧不好)

我想知道我应该采用什么方法来实现这个目标?正如我所说,我希望能够设置每条线的行高,但它们都应该来自圆圈。

另外需要注意的是,每条线的高度都会经常更新,因此快速的方法也会很好。

任何建议都将不胜感激!

好的,所以我尝试使用Polar to Cartesian方法。但这些线条似乎并没有以平等的方式摆放。而是看起来像这样(注意左下角)

enter image description here

这是我写的功能。我做错了什么?

public void DrawLines()
    {
        double circleRadius = ellipse.Height / 2;
        int count = 10; // number of lines
        double angleBetween = 360 / count;
        double offset = 100 + circleRadius; // 100 is the margin from left and top in its parent Grid.
        Random random = new Random();


        for (double angle = 0; angle < 360; angle += angleBetween)
        {
            double height = random.Next(20, 40);

            double xStartPos = offset + (Math.Cos(angle) * (circleRadius));
            double yStartPos = offset + (Math.Sin(angle) * (circleRadius));

            double xEndPos = offset + (Math.Cos(angle) * (circleRadius + height));
            double yEndPos = offset + (Math.Sin(angle) * (circleRadius + height));


            Line line = new Line();

            line.X1 = xStartPos;
            line.Y1 = yStartPos;

            line.X2 = xEndPos;
            line.Y2 = yEndPos;

            line.StrokeThickness = 3;
            line.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));

            lines.Add(line); // adding lines to a list so I can change the height later.

            container.Children.Add(line); // adding to the parent grid
        }

    }

1 个答案:

答案 0 :(得分:1)

两个问题:

double angleBetween = 360 / count;

你正在进行整数除法。适用于10行,但你应该使用双重划分。

并且(可能)真正的问题:

for (double angle = 0; angle < 360; angle += angleBetween)
{
    double xStartPos = offset + (Math.Cos(angle) * (circleRadius));

Math.Cos需要弧度,但你提供度数。令人惊讶的是,它只是有点关闭。

试试这样:

public void DrawLines()
{
    double circleRadius = ellipse.Height / 2;
    int count = 10; // number of lines
    double angleBetween = 2 * Math.PI / count;
    double offset = 100 + circleRadius; // 100 is the margin from left and top in its parent Grid.
    Random random = new Random();


    for (int i = 0; i < count; i++)
    {
        double angle = i * angleBetween;
        double height = random.Next(20, 40);

        double xStartPos = offset + (Math.Cos(angle) * (circleRadius));
        double yStartPos = offset + (Math.Sin(angle) * (circleRadius));