2009-12-04 29 views
1

我正在嘗試使用C#在Silverlight中創建Aitoff-Hammer網格。它應該看起來像this減去點和數字。以編程方式在Silverlight中使用C#繪圖

我不是程序員,但已經能夠使用ActionScript文件將this拼在一起做同樣的事情,這是我的前任編寫的。正如你所看到的,我得到了網格和不需要的對角線。我不確定如何避免在我的代碼中繪製對角線。

任何人可以提供幫助解決我的問題或指出我可能會做錯什麼將不勝感激。如果我遺漏了重要信息,請讓我知道。謝謝。

這裏是我的代碼:

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

是否有一個原因是你在代碼中而不是在XAML中這樣做? – Jermismo

+0

我將在網格中添加元素,如果我可以通過編程方式處理網格上的點,我認爲會更容易。 – Kamal

回答

4

對角線多餘的線條,因爲你要添加點和線段的數字相同。筆從不會從紙上擡起。你必須將你的幾何分解成更多的數字。

將您的細分和圖形創建複製到每一個你想讓筆「下去」的地方,然後將figure.Segments.Add(細分)和geometry.Figures.Add(圖)放到你想要筆的每個地方「上」。

這樣,你的幾何圖形將由許多獨立的圖形組成,並且你的對角線不應該再成爲問題。

 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

不幸的是,這種方法似乎不起作用。可能是因爲即使我在每個循環中都創建了一個新的PolyLineSegment(),總體數字仍然會加入這些點。我可以做每個小節,但這將是很多代碼,與單獨的變量做同樣的事情。不是最好的方式,但我想如果沒有其他選擇,我可能不得不忍受它。 – Kamal

+0

我錯了。將這些數字分成不同的部分是不夠的。圖中的分段是隱式連接的。你實際上必須把你的幾何圖形分成更多的圖形。我的事件測試了它,現在看起來相當不錯。 – Guge

+1

古格,你是人!謝謝一堆!這真的幫了我。 – Kamal

相關問題