Blog RSS

The thought-stream of Tom Medhurst

Archive

Apr
30th
Thu
permalink

Formulae for getting the point co-ordinates for a circle

Here’s how to get the co-ordinates of a given point in a circle…
NOTE: this code is written in C#

Point GetPointPos(int numofpoints, int pointNum, int radius, int offset)
{
  double angle = 2*Math.PI * ((double)pointNum / numofpoints);
  double x = radius * Math.Cos(angle) + offset;
  double y = radius * Math.Sin(angle) + offset;
  return new Point((int)x, (int)y);
}

With this code, we can now create a geometric pattern by calling the method above like so..
NOTE: this code has been tested on MS .NET 2.0

Random r = new Random();
e.Graphics.Clear(Color.White);
const int numofpoints = 30;
const int radius = 200; // pixels
const int offset = 0; // pixels

for (int i = 0; i < numofpoints; i++)
{
  Point pos1 = GetPointPos(numofpoints, i);
  Color colour = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));

  for (int y = i; y < numofpoints; y++)
  {
    Pen pen = new Pen(colour, 1);
    Point pos2 = GetPointPos(numofpoints, y, radius, offset);
    e.Graphics.DrawLine(pen, pos1, pos2);
  }
}

Which generates this…

Geometric Pattern
Comments (View)
blog comments powered by Disqus