using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace 3DRotation {
class Program : Form {
Font f;
float mouseX, mouseY;
float oldMouseX, oldMouseY;
Point3D[] points;
float pointOffsetX = 50;
float pointOffsetY = 50;
public Program() {
f = new Font("Courier New", 12);
points = new Point3D[8];
points[0] = new Point3D(-25, -25, -25);
points[1] = new Point3D(-25, 25, -25);
points[2] = new Point3D(25, -25, -25);
points[3] = new Point3D(25, 25, -25);
points[4] = new Point3D(-25, -25, 25);
points[5] = new Point3D(25, -25, 25);
points[6] = new Point3D(-25, 25, 25);
points[7] = new Point3D(25, 25, 25);
this.MouseMove += new MouseEventHandler(onMouseUp);
this.Paint += new PaintEventHandler(painter);
this.DoubleBuffered = true;
}
public void onMouseUp(object sender, MouseEventArgs e) {
oldMouseX = mouseX;
oldMouseY = mouseY;
mouseX = e.X;
mouseY = e.Y;
foreach (Point3D point in points) {
point.rotateX((oldMouseX - mouseX) / 100);
point.rotateY((oldMouseY - mouseY) / 100);
}
this.Text = "Mouse Position: " + mouseX + ", " + mouseY;
this.Invalidate();
}
public void painter(object sender, PaintEventArgs e) {
Pen p = new Pen(Brushes.Black, 1);
foreach (Point3D point in points) {
e.Graphics.FillEllipse(Brushes.Black, (float)point.x + pointOffsetX, (float)point.y + pointOffsetY, 5, 5);
}
}
static void Main(string[] args) {
Application.Run(new Program());
}
class Point3D {
public double x, y, z;
public Point3D(double x = 0d, double y = 0d, double z = 0d) {
this.x = x;
this.y = y;
this.z = z;
}
//In rotateX, the point is rotated around the X-axis at 0, 0.
//x should remain the same, while y and z rotate.
public void rotateX(double radians) {
System.Console.WriteLine(radians);
y = (y * Math.Cos(radians)) - (z * Math.Sin(radians));
z = (y * Math.Sin(radians)) + (z + Math.Cos(radians));
}
//In rotateY, the point is rotated around the Y-axis at 0, 0.
//y should remain the same, while x and z rotate.
public void rotateY(double radians) {
x = (x * Math.Cos(radians)) - (z * Math.Sin(radians));
z = (x * Math.Sin(radians)) + (z + Math.Cos(radians));
}
}
}
}
I'm using my own 3D point class to hold the x, y, and z values, but when I draw it to the screen I only really use x and y. (I will use z later for scale)
Can someone who knows 3D rotation functions tell me what I am doing wrong?