### C# Spirograph Point Generators

Spirograph's  are cool.  See here and here.

I put together three ways to generate points for a Spirograph, first using a Brute Force straight generate the points, second using a Parallel.For and third using LINQ.

Two important classes first.  First is a static DoubleExtension class to help determine when two doubles are about equal:

[csharp]
using System;

namespace DC.SpiroGraph.Core
{
public static class DoubleExtension
{
public static bool AboutEqual(double x, double y)
{
double epsilon = Math.Max(Math.Abs(x), Math.Abs(y)) * 1E-15;

var variance = x > y ? x - y : y - x;
return Math.Abs(variance)
}
}
}
[/csharp]

Second, a simple Point class. Yeah, I probably could have used the Drawing Point class.

[csharp]
using System;
using System.Diagnostics;

namespace DC.SpiroGraph.Core
{
[DebuggerDisplay("X = {X}, Y = {Y}")]
public class Point
{
public double X { get; set; }
public double Y { get; set; }

{
}
}
}
[/csharp]

Note the use of DebuggerDisplay to help with the debugging.
The SpiroGraphGenerator:

[csharp]
using System;
using System.Collections.Generic;
using System.Linq;

namespace DC.SpiroGraph.Core
{
public class SpiroGraphGenerator
{
/// <summary>
/// Radius, R, of Circle (equator) centered at the origin
/// </summary>
public double Radius1 { get; set; }

/// <summary>
/// Radius, r, of circle (bicyle wheel) cetered at (R + r, 0)
/// </summary>
public double Radius2 { get; set; }

/// <summary>
/// Distance of Point (reflector) from the center of Circle 2 (the circle of Radius2)
/// </summary>
public double Position { get; set; }

/// <summary>
/// Controls how precise the SpiroGraph is drawn.
/// Controls t of f(t).
/// Resolution = 360, t = 0, 1, 2, 3, ...,360
/// Resolution = 180, t = 0, 2, 4, 6, ... 360
///
/// </summary>
public double Resolution { get; set; }

public IEnumerable<Point> GetSpiroGraphPoints()
{

var t = 0d;

var currentPoint = new Point { X = firstPoint.X, Y = firstPoint.Y };

var increment = (360 / Resolution) * Math.PI / 180;

do
{
for (var i = 0; i < Resolution; i++)
{
yield return currentPoint;
t += increment;
}

yield return currentPoint;
}

public IEnumerable<Point> GetSpiroGraphPoints2()
{
var endPoints = FindAllEndPoints();
var numberOfEndPonits = endPoints.Count();
var numberOfPoints = Convert.ToInt32((numberOfEndPonits - 1)*Resolution + 1);
var points = new Point[numberOfPoints];
var resolution = Convert.ToInt32(Resolution);

var increment = (360/Resolution)*Math.PI/180;

Parallel.For(1, numberOfEndPonits, i =>
{
for (var j = 0; j < resolution; j++)
{
var pointIndex = (i - 1)*resolution + j + 1;
var t = pointIndex*increment;
}
});
return points;
}

public IEnumerable<Point> GetSpiroGraphPoints3()
{
var endPoints = FindAllEndPoints();
var numberOfEndPonits = endPoints.Count();
var numberOfPoints = Convert.ToInt32((numberOfEndPonits - 1)*Resolution + 1);

var increment = (360/Resolution)*Math.PI/180;

var points2 = Enumerable.Range(0, numberOfPoints)
.Select(i => new {Index = i, t = i*increment})
.Select(it => new {it.Index, point = GetPoint(sumOfRadius, it.t)})
.OrderBy(ip => ip.Index)
.Select(ip => ip.point);

return points2;
}

public IEnumerable<Point> FindAllEndPoints()
{
var t = 0d;

var currentPoint = new Point { X = firstPoint.X, Y = firstPoint.Y };
var increment = CalculateIncrement();

do
{
yield return currentPoint;
t += (increment*Resolution);

yield return currentPoint;
}

private double CalculateIncrement()
{
return (360/Resolution)*Math.PI/180;
}

{
}

private static double Xt(double sumOfRadius, double radius2, double position , double t)
{
}

{
}

private Point GetPoint(double sumOfRadius, double t)
{
}
}
}
[/csharp]

I created a simple WinForm app using DevExpress controls, this is how I populated the graph with points:

[csharp highlight="28,31"]
using System;
using System.Linq;
using System.Windows.Forms;

namespace DC.SpiroGraph.WinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void CreateData(bool useParallel)
{
try
{
Cursor.Current = Cursors.WaitCursor;

var sg = new Core.SpiroGraphGenerator
{
Position = Convert.ToDouble(positionTextEdit.Text),
Resolution = Convert.ToDouble(resolutionTextEdit.Text)
};

var endPoints = sg.FindAllEndPoints();
endPointCountLabel.Text = string.Format("# of End Points: {0}", endPoints.Count());

var graphPoints = useParallel? sg.GetSpiroGraphPoints2() : sg.GetSpiroGraphPoints3();
pointCountLabel.Text = string.Format("# of Points: {0}", graphPoints.Count());

chartControl1.Series[0].DataSource = graphPoints;

}
catch (Exception)
{

// Eat It
}
finally
{
Cursor.Current = Cursors.Default;
}
}

private void Form1_Load(object sender, EventArgs e)
{
var series = chartControl1.Series[0];
series.Label.Visible = false;
series.ArgumentDataMember = "X";

positionTextEdit.Text = "60";
resolutionTextEdit.Text = "270";

CreateData(true);
}

private void simpleButton1_Click(object sender, EventArgs e)
{
CreateData(true);
}

private void simpleButton2_Click(object sender, EventArgs e)
{
CreateData(false);
}
}
}
[/csharp]

Future changes will be to make a series for each rotation about the main circle, this way we can start adding different colors to the graph.

### Json for jqGrid from ASP.Net MVC

jqGrid takes a specific format for its json (taken from jqGrid documentation): [js]{ total: "xxx",page: "yyy", records: "zzz", rows : [ {id:"1", cell:["cell11", "cell12", "cell13"]}, {id:"2", cell:["cell21", "cell22", "cell23"]}, ... ]}[/js] The tags mean the following: total - Total number of Pages. page - Current page Index. records - Total number of records in the rows group. rows - An array with the data plus an identifier. id - The unique row identifier, needs to be an int from what I have found. cell - An array of the data for the grid. The ASP.Net MVC framework has the JsonResult response type which we can use to populate the jqGrid. As an example I created a Person model and a method to return some data: [csharp] public class Person { public int ID { get; set; } public string Name { get; set; } public DateTime Birthday { get; set; } } public I

### Remote Controlled RoboTank

This is my version of the ever popular to build RoboTank. It uses an Arduino Mega 2560 with the AdaFruit motor shield and an XBee S1 to communicate to the DFRobot Gamepad. The sketch for the RoboTank makes use of the AFMotor.h to drive the motors and includes a serial parser to read and process the commands coming from the Gamepad. Robotank-Sketch.zip DFRobot Wireless Joystick

### Changing Typed DataSet Connection String

I was working on a WinForm app that connected to a MS Access database. Yeah, Access sucks but I didn't have a choice in the matter. The app itself is used to import a bunch of CSV files into the Access database. It is more of a utility program and it has going through several variations, from being a simple hand driven command line tool to being GUI driven. The command line version was all hand controlled. I had to go in and update configuration files to point to the CSV files and the MDB database. That got old fast so I decided to make a GUI version that would allow me to pick the MDB file and each of the CSV files to import. Picking and using the CSV files was easy, it was changing the connection string for the MDB that proved to be the hardest. I am using strongly typed datasets in VS2005. If you have ever worked with them you find out soon that the connection string gets saved with the project in the app.config file, even if it is a seperate DAL dll project. My guess is