Skip to main content
 首页 » 编程设计

c#之在 Silverlight 中使用 C# 以编程方式绘制

2024年02月24日19jpfss

我正在尝试使用 C# 在 Silverlight 中创建 Aitoff-Hammer 网格。它应该看起来像 this减去点和数字。

我不是程序员但能够拼凑this使用 ActionScript 文件执行我的前任编写的相同操作。 如您所见,我得到了网格加上不需要的对角线。我不确定如何避免在我的代码中绘制对角线。

任何人都可以提供任何帮助来解决我的问题或指出我可能做错了什么,我们将不胜感激。如果我遗漏了重要信息,请告诉我。谢谢。

这是我的代码:

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

请您参考如下方法:

有多余的对角线是因为您要向同一个图形添加点和线段。笔永远不会离开纸。您必须将几何体拆分成更多图形。

将创建的线段和图形复制到您希望笔“向下”的每个位置,并将 figure.Segments.Add(segment) 和 geometry.Figures.Add(figure) 复制到您希望笔“向下移动”的每个位置上去”。

这样你的几何图形将由许多独立的图形组成,你的对角线应该不再是问题。

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