Hi guys,

I'm having a bit of trouble with a bit of trig in C#. I've written code similar to this in actionscript3 (flash), but it is giving me issues in C#. I am not receiving errors, but all the shapes I rotate are somewhat screwed up.

Here is the points array I am using:

``static private Point[] drawingPoints = new Point{new Point(14,0),new Point(19,9),new Point(19,18),new Point(25,30),new Point(19,26),new Point(19,18),new Point(19,26),new Point(8,26),new Point(8,18),new Point(0,30),new Point(8,26),new Point(8,9),new Point(14,0)};``

Here is the rotation code + the code to draw this.

``````public void draw (Graphics g)
{
Point[] rotatedPoints = new Point;

for (short index = 0; index < rotatedPoints.Length; index++)
{
//Finding the location of the points relative to the upper left of the ship.
Point curPoint = drawingPoints[index];
Point newPoint = new Point(curPoint.X,curPoint.Y);

double distToPoint = Form1.distTo(curPoint.X,curPoint.Y,centerPoint.X,centerPoint.Y);//we treat this as the hypotenuse.

decimal degToPoint = (decimal)Math.Atan(centerPoint.Y - curPoint.Y / centerPoint.X - curPoint.X)/Form1.rad;//Gets the degrees from the center point to the current drawing point without any rotation yet.
decimal degToNewPoint = degToPoint + rotation;//Add the rotation

decimal cosToNewPoint = (decimal)Math.Cos((double)(degToNewPoint*Form1.rad));//From the center the cosine.

newPoint.X = (int)(cosToNewPoint * (decimal)distToPoint);

decimal sinToNewPoint = (decimal)Math.Sin((double)(degToNewPoint * Form1.rad));//From the center the cosine.

Debug.Fail(sinToNewPoint.ToString());

//Same idea as with the cosine
newPoint.Y = (int)(sinToNewPoint * (decimal)distToPoint);

rotatedPoints[index] = newPoint;
}
//Adding the screen x y location to the points:

for (short i = 0; i < rotatedPoints.Length; i++)
{
rotatedPoints[i].X += this._x;
rotatedPoints[i].Y += this._y;
}
Pen shipPen = new Pen(shipColor, 2);
g.DrawLines(shipPen, rotatedPoints);
}``````

I know that the drawing code works correctly. If you comment out the rotation parts of the code, it displays my drawing just fine. What is annoying, is that if the rotation is something other than zero, the rotation works, but the shape is still flattened/scewed on one side.

There is a native matrix transform that you can use in the System.Drawing.Drawing2D namespace.
Below is an example of how I might do it using a GraphicsPath.
The draw method could be adapted to plot any GraphicsPath with rotation and location parameters.

``````// GraphicPath to hold basic ship shape
System.Drawing.Drawing2D.GraphicsPath shipShape;
// My ship color
Color shipColor = Color.Blue;
// basic ship point array
static private Point[] drawingPoints = new Point[] { new Point(13, 0), new Point(19, 9), new Point(19, 18), new Point(25, 30), new Point(19, 26), new Point(19, 18), new Point(19, 26), new Point(8, 26), new Point(8, 18), new Point(0, 30), new Point(8, 26), new Point(8, 9), new Point(13, 0) };
// rotation angle to apply to ship when drawn.
float shipAngle = 90f;
// offset of ship center point from drawing origin.
Point shipLocation = new Point(20,20);

// creates the ship GraphicsPath
private void createShipShape()
{
shipShape = new System.Drawing.Drawing2D.GraphicsPath();
shipShape.CloseAllFigures();
// offset to make origin at ship center
// this would be better done directly by adjusting drawingPoints using -ve numbers
System.Drawing.Drawing2D.Matrix offsetMatrix = new System.Drawing.Drawing2D.Matrix();
offsetMatrix.Translate(-13, -15);
shipShape.Transform(offsetMatrix);
}

private void draw(Graphics g)
{
// set up ship graphic if not alread done (this could/should be done elsewhere)
if (shipShape == null)
{
createShipShape();
}

// use local temp GraphicsPath to preserve original path
// required because Tranform later changes path
System.Drawing.Drawing2D.GraphicsPath shipDrawPath =
new System.Drawing.Drawing2D.GraphicsPath();

// define rotation (and offset) matrix
System.Drawing.Drawing2D.Matrix rotateMatrix = new System.Drawing.Drawing2D.Matrix();