Reputation: 11
I'm trying to display the 3 dimensional axis using the drawline method, I've managed to make a line spin around its axis but I'm having trouble making the second line spin in a 90 degree angle from that line, I would like to implement the basic rotation method but I don't know how to implement it in c# http://en.wikipedia.org/wiki/Rotation_matrix . I would like to know how do I place the z axis on the xy chart. Remember I want to do this in plain c# no direct x or anything.
here is my basic code for drawing
private void Go_Click(object sender, EventArgs e)
{
int radiusx1= Int32.Parse(xbarvalue.Text);
int radiusy1 = Int32.Parse(ybarvalue.Text);
int radiusz1 = Int32.Parse(zbarvalue.Text);
int speedwait = speed.Value;
int position1 =canvas.Width / 2 ;
int position2 = canvas.Height / 2;
for (double i = 0; i <=360; i = i + 0.5)
{
double angle= Math.PI * i / 180.0;
// double angle = i * 0.01745;
float position1x = (float)( position1+Math.Sin(angle)*Theta);
float position1y = (float)( position2+Math.Cos(angle)*Theta2);
float position2x = (float)(position1 + Math.Sin(angle) *radiusy1);
float position2y = (float)(position2 + Math.Cos(angle) * radiusz1);
float position3x = (float)(position1 + Math.Sin(angle) *radiusx1);
float position3y = (float)(position2 + Math.Cos(angle) *radiusz1);
// Graphics g;
// g = canvas.CreateGraphics();
//Pen p;
// Rectangle r;
// p = new Pen(Brushes.Blue);
// r = new Rectangle(position2x,position2y, 1, 1);
// g.DrawRectangle(p, r);
System.Threading.Thread.Sleep(speedwait);
canvas.Refresh();
px.Text = Convert.ToString(position1x);
py.Text = Convert.ToString(position1y);
// pz.Text = Convert.ToString(position1z);
px.Refresh();
py.Refresh();
// pz.Refresh();
System.Drawing.Pen myPen;
System.Drawing.Pen myPen2;
System.Drawing.Pen myPen3;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
myPen2 = new System.Drawing.Pen(System.Drawing.Color.Black);
myPen3 = new System.Drawing.Pen(System.Drawing.Color.Blue);
System.Drawing.Graphics formGraphics = canvas.CreateGraphics();
formGraphics.DrawLine(myPen, position1, position2, position1x, position1y);
formGraphics.DrawLine(myPen2, position1, position2,position2x,position2y);
formGraphics.DrawLine(myPen3, position1, position2,position3x, position3y);
};
}
my first method was to use ellipses on spinning lines to give the illusion of depth
could someone show me how to write the basic rotation code in c#, I'm having problems with the arrays.
Upvotes: 0
Views: 1784
Reputation: 3821
If you add references to WindowsBase and PresentationCore then there are objects which can help you with 3D transformations. These assemblies are part of the basic .NET Framework and do not require any additional libraries to be installed like DirectX. Example:
using System.Windows.Media.Media3D;
...
// Define a 90 degree rotation about Y axis
var rotation = new AxisAngleRotation(new Vector(0,1,0), 90);
// Create transformation from our rotation definition
var transform = new RotateTransform3D(rotation);
// Rotate a point using the transformation
var original = new Vector3D(1, 1, 1);
var rotated = transform.Transform(original);
In order to draw 3D objects on a 2D surface, though, you will have to use a projection matrix. A perspective projection makes objects look smaller far away while a parallel projection preserves size at all distances. The projection matrix is the final step which converts 3D vectors into 2D vectors that are usable on the screen.
Upvotes: 1