Geometric Art using Triangles (C#)

vegaseat 0 Tallied Votes 167 Views Share

Remember the Spirograph? It used geared circles to create some rather artistic geometric designs. This program uses triangles to follow in the Spirograph's foot steps. The math is rather simple, just a couple of sine and cosine functions, also the ubiquitous for loops, everything wrapped up in the using() statement. This is a Windows application.

// experimenting with math generated graphics
// MS Visual C# .NET  by  vegaseat  16mar2005
//
// this is a Windows Application

using System;
using System.Drawing;
using System.Runtime.InteropServices;  // DllImport()
using System.Windows.Forms;

namespace DrawSpokes1
{
	public class Form1 : System.Windows.Forms.Form
	{
    // WinApi Sleep() is in kernel32.dll  
    [DllImport("kernel32")]
    private static extern void Sleep(uint dwMilliseconds);

		public Form1()
		{
			InitializeComponent();
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
      this.ClientSize = new System.Drawing.Size(640, 344);
      this.Name = "Form1";
      this.Text = "Draw Spokes ...";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
		#endregion

		static void Main() 
		{
			Application.Run(new Form1());
		}

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      // note: using() calls dispose automatically
      using (Pen p = new Pen(Brushes.Green,1))
      {
        int k, x, y;
        int spokes;  // triangle = 3,  more complex > 3

        for (k = 3; k < 12; k++)
        {
          spokes = k;
          this.Text = "Draw Spokes = " + spokes;
          double radians = 360 / (spokes * 57.29578);
          for (x = 1; x <= spokes; x++) 
          {
            for (y = x; y <= spokes; y++) 
            {
              e.Graphics.DrawLine(p,(int)(Math.Sin(y * radians) * 225 + 320),
                (int)(Math.Cos(y * radians) * 145 + 150),
                (int)(Math.Sin(x * radians) * 225 + 320),
                (int)(Math.Cos(x * radians) * 145 + 150));
            }
          }
          Sleep(3000);
        }
      }
      this.Text = "Done! Is there a heart in there?";
    }
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.