开发者

Drawing programmatically using C# in Silverlight

I am trying to create an Aitoff-Hammer grid in Silverlight using C#. It should look like this minus the dots and numbers.

I am not a programmer but have been able to piece together this using an ActionScript file to do the same thing that was written by my predecessor. As you can see, I get the grid plus unwanted diagonal lines. I am not sure how to avoid having the diagonal lines from being drawn in my code.

Any help anyone can provide to fix my problem or point out what I may be doing wrong will be much appreciated. Please let me know if I left out important information. Thanks.

Here is my code:

PolyLineSegment segment = new PolyLineSegment();

PathFigure figure 开发者_如何学编程= new PathFigure();

figure.StartPoint = new Point(xCenter, yCenter);

PathGeometry geometry = new PathGeometry();

Path path = new Path();

path.Stroke = new SolidColorBrush(Colors.Black);

path.StrokeThickness = 2;
aitoff coords = new aitoff(); 

for (int ra = 0; ra <= 24; ra = ra + 3)
{
    for (int dec = -90; dec <= 90; dec = dec + 3)
    {
        points = coords.GetAitoffCoord(ra, dec);
        double xCoord = xCenter + points.X * width / 2;
        double yCoord = yCenter + points.Y * height / 2;
        segment.Points.Add(new Point(xCoord, yCoord));                  
    }
}      

for (int dec = -90; dec <= 90; dec = dec + 30)
{
    for (int ra = 0; ra <= 12; ra = ra + 1)
    {
        points = coords.GetAitoffCoord(ra, dec);
        double xCoord = xCenter + points.X * width / 2;
        double yCoord = yCenter + points.Y * height / 2;                  
        segment.Points.Add(new Point(xCoord, yCoord));
    }
}

for (int dec = -90; dec <= 90; dec = dec + 30)
{
    for (double ra = 12.01; ra <= 25; ra++)
    {
        points = coords.GetAitoffCoord(ra, dec);
        double xCoord = xCenter + points.X * width / 2;
        double yCoord = yCenter + points.Y * height / 2;
        segment.Points.Add(new Point(xCoord, yCoord));
    }
}

for (int dec = -90; dec <= 90; dec = dec + 3)
{
    double ra = 12.01;

    points = coords.GetAitoffCoord(ra, dec);
    double xCoord = xCenter + points.X * width / 2;
    double yCoord = yCenter + points.Y * height / 2;
    segment.Points.Add(new Point(xCoord, yCoord));
}

figure.Segments.Add(segment);
geometry.Figures.Add(figure);
path.Data = geometry;
LayoutRoot.Children.Add(path);


// GetAitoff

public class aitoff
{
    double ra;
    double dec;

    Point coords = new Point();

    double ra2deg = Math.PI / 180.0f;

    public Point GetAitoffCoord(double raIn, double decIn)
    {
        ra = raIn * 360 / 24;
        dec = decIn;
        if (ra > 180)
           ra = ra - 360;

        double l = ra * ra2deg;
        double b = dec * ra2deg;

        double t = Math.Sqrt(2 / (1 + Math.Cos(b) * Math.Cos(l / 2)));
        double x = 2 * t * Math.Cos(b) * Math.Sin(l / 2);
        double y = t * Math.Sin(b);

        coords.X = x / (-2 * Math.Sqrt(2));
        coords.Y = y / (-1 * Math.Sqrt(2));

        return coords;
    }
}


The diagonal superfluous lines are there because you are adding points and segments to the same figure. The pen never gets lifted from the paper. You have to split your geometry into more figures.

Copy your segment and figures creating into every place you want the pen to "go down", and the figure.Segments.Add(segment) and geometry.Figures.Add(figure) into every place you want the pen to "go up".

That way your geometry will consist of many separate figures, and your diagonals should no longer be a problem.

        for (int ra = 0; ra <= 24; ra = ra + 3)
        {
            figure = new PathFigure();
            segment = new PolyLineSegment();
            for (int dec = -90; dec <= 90; dec = dec + 3)    
            {        
                points = coords.GetAitoffCoord(ra, dec);        
                double xCoord = xCenter + points.X * width / 2;        
                double yCoord = yCenter + points.Y * height / 2;        
                segment.Points.Add(new Point(xCoord, yCoord));                      
            }
            figure.StartPoint = segment.Points[0];
            figure.Segments.Add(segment);
            geometry.Figures.Add(figure);
        }

        for (int dec = -90; dec <= 90; dec = dec + 30)
        {
            figure = new PathFigure();
            segment = new PolyLineSegment();
            for (int ra = 0; ra <= 12; ra = ra + 1)
            {
                points = coords.GetAitoffCoord(ra, dec);
                double xCoord = xCenter + points.X * width / 2;
                double yCoord = yCenter + points.Y * height / 2;
                segment.Points.Add(new Point(xCoord, yCoord));
            }
            figure.StartPoint = segment.Points[0];
            figure.Segments.Add(segment);
            geometry.Figures.Add(figure);
        }

        for (int dec = -90; dec <= 90; dec = dec + 30)
        {
            figure = new PathFigure();
            segment = new PolyLineSegment();
            for (double ra = 12.01; ra <= 25; ra++)
            {
                points = coords.GetAitoffCoord(ra, dec);
                double xCoord = xCenter + points.X * width / 2;
                double yCoord = yCenter + points.Y * height / 2;
                segment.Points.Add(new Point(xCoord, yCoord));
            }
            figure.StartPoint = segment.Points[0];
            figure.Segments.Add(segment);
            geometry.Figures.Add(figure);
        }

        figure = new PathFigure();
        segment = new PolyLineSegment();
        for (int dec = -90; dec <= 90; dec = dec + 3)
        {
            double ra = 12.01;
            points = coords.GetAitoffCoord(ra, dec);
            double xCoord = xCenter + points.X * width / 2;
            double yCoord = yCenter + points.Y * height / 2;
            segment.Points.Add(new Point(xCoord, yCoord));
        }
        figure.StartPoint = segment.Points[0];
        figure.Segments.Add(segment);
        geometry.Figures.Add(figure);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜