As another example of what you can do with graphics in C#, I decided to recreate a famous Turtle Graphics Spiral design. I can’t find an official name for this classic design. I decided to call it Spiral of Circles. It has a nautilus shape but you won’t find any sample code by doing a search on “Turtle Graphics nautilus”. There is a version of this design using squares. I did have the code for creating this design using Python or Processing. It took a little experimentation to reproduce it in C#. Part of the solution was to realize that radians needed to be converted to degrees which is why I included the conversion functions in the code. This design requires a rotate and a translate transformation within a loop. I’m not sure you really need to use a GraphicsPath object because I tried that before I solved the other problems.
Related Articles
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SpiralOfCircles
{
public partial class Form1 : Form
{
System.Drawing.Graphics graphicsObj;
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
graphicsObj = this.CreateGraphics();
// Set the SmoothingMode property to smooth the lines.
graphicsObj.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// This centers what is drawn. Adjusted by 50 pixels.
graphicsObj.TranslateTransform((graphicsObj.VisibleClipBounds.Width / 2) + 50, (graphicsObj.VisibleClipBounds.Height / 2) - 50, MatrixOrder.Append);
// This scales everything up
graphicsObj.ScaleTransform(2, 2);
// Create pen.
Pen blackPen = new Pen(Color.Black, 1);
for (int i = 0; i
/// Function to convert degrees to radians
///
/// degree value
/// equivalent radian value
public static double radians(double degrees)
{
double radians = (Math.PI / 180) * degrees;
return (radians);
}
///
/// Function to convert radians to degree
///
/// radian value
/// equivalent degree value
public static double degrees(double radians)
{
double degrees = (180 / Math.PI) * radians;
return (degrees);
}
}
}
This post first appeared on Williamsport Web Developer Weblog | Brief Notes On, please read the originial post: here