2015-11-17 66 views
-2

我是一名在学校学习C#的学生,在我们的图形部门有一个项目到期。我用点创建了一棵圣诞树并填充它。现在我正在寻找在我已经在树中声明的范围内创建椭圆形装饰物。有没有办法让这些椭圆只在我的树内,并让它们根据树中的随机数生成器进行更改?谢谢。如何在设定范围内的随机点绘制椭圆?

这是我的代码树。我制作的椭圆是雪花。 SolidBrush green = new SolidBrush(Color.Green);

Pen greentree = new Pen(Color.Green); 

    Point[] christmastree = new Point[11]; 
    christmastree[0] = new Point(518, 400); 
    christmastree[1] = new Point(620, 300); 
    christmastree[2] = new Point(549, 300); 
    christmastree[3] = new Point(645, 185); 
    christmastree[4] = new Point(607, 185); 
    christmastree[5] = new Point(673, 102); 
    christmastree[6] = new Point(744, 185); 
    christmastree[7] = new Point(706, 185); 
    christmastree[8] = new Point(793, 300); 
    christmastree[9] = new Point(720, 300); 
    christmastree[10] = new Point(835, 400); 
    g.DrawPolygon(greentree, christmastree); 
    g.FillPolygon(green, christmastree); 

    //Snow 
    Random r = new Random(); 
    SolidBrush snowsb = new SolidBrush(Color.White); 
    for(int i = 1; i <= 40; i++) 
    { 
     int snowflake_x = r.Next(1000); 
     int snowflake_y = r.Next(500); 
     g.FillEllipse(snowsb, snowflake_x, snowflake_y, 4,4); 
    } 

就像我说的,我在这个C#领域非常缺乏经验。谢谢

+2

你应该张贴你的树和/或你已经为椭圆做的一些代码。是的,这是可能的,但我们需要更好地理解我们正在帮助您的工作。 –

+0

你的目标是什么:Winforms? WPF? ASP? ...? __Always__相应地标记您的问题!如果你有一个点的多边形列表,你可以使用它制作的图形路径,并测试GP.IsVisible(Point),看看你的椭圆的中心是否在树区域.. – TaW

+0

刚刚发布我的代码。非常感谢 – CJH

回答

0

尝试类似的东西:

Random r = new Random(); 

// Number of ellipses 
int ellipseCount = 0; 

// Loop for setting a specified amount of ellipses 
while (ellipseCount < 10) 
{ 

    // New random point 
    Point p = new Point(r.Next(0, 300), r.Next(0,300)); 
    // check if point is in my range 
    if (p.IsInMyRange()) 
    { 
     Ellipse e = new Ellipse {Width = 10, Height = 10}; 
     // Implement: set coordinates to ellipse 

     ellipseCount++; 
    } 
} 

你需要实现,如果你的问题是在你的范围内,其测试的方法。没有你的代码,我无法帮助你。

0

尝试......

namespace Baum 
{ 
    using System; 
    using System.Drawing; 
    using System.Drawing.Drawing2D; 
    using System.Windows.Forms; 

    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      base.OnPaint(e); 

      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 

      PointF[] tree = new PointF[] 
      { 
       new PointF(12, 0), 
       new PointF(16, 4), 
       new PointF(13, 4), 
       new PointF(19, 11), 
       new PointF(13, 11), 
       new PointF(21, 20), 
       new PointF(3, 20), 
       new PointF(11, 11), 
       new PointF(5, 11), 
       new PointF(11, 4), 
       new PointF(8, 4) 
      }; 

      PointF[] stump = new PointF[] 
      { 
       new PointF(10, 20), 
       new PointF(14, 20), 
       new PointF(14, 27), 
       new PointF(19, 27), 
       new PointF(19, 30), 
       new PointF(6, 30), 
       new PointF(6, 27), 
       new PointF(10, 27) 
      }; 

      using (GraphicsPath path = new GraphicsPath()) 
      { 
       path.AddPolygon(tree); 
       Matrix m = new Matrix(); 
       m.Scale(20, 20); 
       path.Transform(m); 
       e.Graphics.FillPath(Brushes.Green, path); 

       foreach (PointF p in path.PathPoints) 
       { 
        int s = 15; 
        PointF q = p; 
        q.X -= (s/2); 
        q.Y -= (s/2); 

        if (new Random(Guid.NewGuid().GetHashCode()).Next(0, tree.Length) > tree.Length/2) 
        { 
         e.Graphics.FillEllipse(Brushes.Red, new RectangleF(q.X, q.Y, s, s)); 
        } 
       } 
      } 

      using (GraphicsPath path = new GraphicsPath()) 
      { 
       path.AddPolygon(stump); 
       Matrix m = new Matrix(); 
       m.Scale(20, 20); 
       path.Transform(m); 
       e.Graphics.FillPath(Brushes.Brown, path); 
      } 

      System.Threading.Thread.Sleep(500); 
      Refresh(); 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 
    } 
} 
+0

为什么不使用GraphicsPath.IsVisible函数? – TaW