Last active
January 3, 2016 20:49
-
-
Save sushovande/8517261 to your computer and use it in GitHub Desktop.
SpiroGifsCreates the frames of a [Spirograph](http://en.wikipedia.org/wiki/Spirograph) animation. Modify the variables in lines 12-22 for various patterns. After stitching the resulting images into a gif, they look like this: http://imgur.com/FPrWHOw, http://imgur.com/pFeMozI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Drawing; | |
namespace SpiroGifs | |
{ | |
class Program | |
{ | |
const int BmpWidth = 300; | |
const int BmpHeight = 300; | |
List<PointF> pointlist = new List<PointF>(); | |
List<List<PointF>> ppl = new List<List<PointF>>(); | |
const int numppl = 11; | |
bool drawfluff = false; | |
float R = 150; | |
float r = 110; | |
float g = 60; | |
const int numFrames = 100; | |
const float FrameRate = 9F; | |
static void Main(string[] args) | |
{ | |
Program p = new Program(); | |
for (int i = 0; i < numppl; i++) | |
{ | |
p.ppl.Add(new List<PointF>()); | |
} | |
for (int i = 0; i < numFrames; i++) | |
{ | |
p.drawFrame(i); | |
} | |
} | |
void drawFrame(int i) | |
{ | |
Bitmap bmp = new Bitmap(BmpWidth, BmpHeight); | |
using (Graphics context = Graphics.FromImage(bmp)){ | |
context.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; | |
context.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; | |
context.Clear(Color.CornflowerBlue); | |
float alpha = i / FrameRate; | |
Pen p = Pens.Beige; | |
if (drawfluff) | |
{ | |
context.DrawEllipse(p, getBoundingRectForEllipse(0, 0, R)); | |
context.DrawEllipse(p, getBoundingRectForEllipse((R-r)*cosf(alpha), (R-r)*sinf(alpha), r)); | |
} | |
for (int k = 0; k < numppl; k++) | |
{ | |
float theta = alpha + k * (numFrames / FrameRate); | |
float x = (R - r) * cosf(theta) + g * cosf(theta * (1 - R / r)); | |
float y = (R - r) * sinf(theta) + g * sinf(theta * (1 - R / r)); | |
//pointlist.Add(new PointF(tx(x), ty(y))); | |
ppl[k].Add(new PointF(tx(x), ty(y))); | |
for (int j = 1; j < ppl[k].Count; j++) | |
{ | |
context.DrawLine(p, ppl[k][j - 1], ppl[k][j]); | |
} | |
} | |
} | |
bmp.Save(string.Format("foo{0:000}.png", i), System.Drawing.Imaging.ImageFormat.Png); | |
} | |
float cosf(float theta) | |
{ | |
return (float)Math.Cos(theta); | |
} | |
float sinf(float theta) | |
{ | |
return (float)Math.Sin(theta); | |
} | |
RectangleF getBoundingRectForEllipse(float x, float y, float r) | |
{ | |
return new RectangleF( | |
tx(x) - r, | |
ty(y) - r, | |
2 * r, | |
2 * r); | |
} | |
float tx(float x) { return x + BmpWidth / 2; } | |
float ty(float y) { return BmpHeight / 2 + y; } | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment