Need to implement clipping regions
Need to change LookAt location....not implemented
It just acts kinda fishy...
Here is my code:
///
/// Converts the 3D vector to a 2D vector. Uses Camera data.
///
///
public Vector2D ConvertToVector2D() {
Vector3D vector = this; // 3D Vector to convert
Vector2D vectorResult = new Vector2D(); // 2D Vector to store result
Vector3D cameraPosition = pC; // Camera Position (default = 0, 0, 50)
Vector3D translation = new Vector3D(); // Temporary vector to store translation for projection
Vector3D theta = new Vector3D(pTheta.X * (Math.PI / 180),
pTheta.Y * (Math.PI / 180),
pTheta.Z * (Math.PI / 180)); // Camera angle / Translate degress to radians
Vector3D viewScreen = pE; // Projection screen location
// Extracted from inline algorithms for debugging purposes
double sinX = Math.Sin(theta.X);
double cosX = Math.Cos(theta.X);
double sinY = Math.Sin(theta.Y);
double cosY = Math.Cos(theta.Y);
double sinZ = Math.Sin(theta.Z);
double cosZ = Math.Cos(theta.Z);
// Translation of coordinates: taken from http://en.wikipedia.org/wiki/3D_projection
translation.X = cosY * (sinZ * (vector.Y - cameraPosition.Y) + cosZ * (vector.X - cameraPosition.X))
- sinY * (vector.Z - cameraPosition.Z);
translation.Y = sinX * (cosY * (vector.Z - cameraPosition.Z) + sinY * (sinZ * (vector.Y - cameraPosition.Y) + cosZ * (vector.X - cameraPosition.X)))
+ cosX * (cosZ * (vector.Y - cameraPosition.Y) - sinZ * (vector.X - cameraPosition.X));
translation.Z = cosX * (cosY * (vector.Z - cameraPosition.Z) + sinY * (sinZ * (vector.Y - cameraPosition.Y) + cosZ * (vector.X - cameraPosition.X)))
- sinX * (cosZ * (vector.Y - cameraPosition.Y) - sinZ * (vector.X - cameraPosition.X));
// Project translated coordinates to 2D screen
// Prevent div by zero
double Zprojection;
if(translation.Z == 0) {
Zprojection = 0;
}
else {
Zprojection = (viewScreen.Z / translation.Z);
}
vectorResult.X = (Math.Round(translation.X, 6) - viewScreen.X) * Zprojection;
vectorResult.Y = (Math.Round(translation.Y, 6) - viewScreen.Y) * Zprojection;
return vectorResult;
}
Robert TenneyPosted Nov 28, 2009, 10:19 PM
Nick WaltersPosted Nov 19, 2009, 4:18 AM
Robert TenneyPosted Dec 26, 2008, 11:13 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
[assembly:CLSCompliant(true)]
namespace TenneySoftware.Vectors {
///
/// A class that represents 3 dimentional double point precision vectors, as well as the math for it.
///
public class Vector3D : Object {
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
private Point pConverted = new Point();
private static bool pScaled;
private static bool pCartCoord;
private static Point pCenter = new Point();
private static double pScaleFactor;
private static Size pScreenSize = new Size();
private Camera pCamera;
private static World pWorld;
private double _x;
private double _y;
private double _z;
public static World ActiveWorld {
get {
return pWorld;
}
set {
pWorld = value;
}
}
public class Camera {
public Camera() {
pC = new Vector3D(0, 0, 0);
pTheta = new Vector3D(0, 0, 0);
pE = new Vector3D(0, 0, 0);
}
public Camera(Vector3D cameraPosition, Vector3D cameraRotation, Vector3D viewScreen) {
pC = cameraPosition;
pTheta = cameraRotation;
pE = viewScreen;
}
private Vector3D pC = new Vector3D(); // camera
private Vector3D pTheta = new Vector3D(); // direction of camera
private Vector3D pE = new Vector3D(); // viewer point
private double pFOV = 0;
public double FOV {
get {
return pFOV;
}
set {
pFOV = value;
}
}
public Vector3D CameraPosition {
get {
return pC;
}
set {
pC = value;
}
}
public Vector3D CameraAngle {
get {
return pTheta;
}
set {
pTheta = value;
}
}
public void LookAt(Vector3D vector, double roll) {
Vector3D camera = this.CameraPosition;
double xAngle = Vector2D.radsToDeg(Math.Atan(camera.X / camera.Z));
double yAngle = Vector2D.radsToDeg(Math.Atan(camera.Y / camera.Z));
pTheta = new Vector3D(xAngle, -yAngle, roll);
}
public void LookAt(Vector3D vector) {
LookAt(vector, 0.0);
}
public Vector3D Projection {
get {
return pE;
}
set {
pE = value;
}
}
}
public class Segment {
private Vector3D pPoint1;
private Vector3D pPoint2;
public Segment(Vector3D point1, Vector3D point2) {
pPoint1 = point1;
pPoint2 = point2;
}
public Vector3D Point1 {
get {
return pPoint1;
}
set {
pPoint1 = value;
}
}
public Vector3D Point2 {
get {
return pPoint2;
}
set {
pPoint2 = value;
}
}
}
public class World {
private List
private List
private List
private bool AddingVectors = false;
private bool AddingSegments = false;
private Vector3D pWorldSize = new Vector3D(); // Size of 3D world
public World() {
pWorldSize = new Vector3D(10000, 10000, 10000);
}
public World(Vector3D worldSize, Camera camera) {
pWorldSize = worldSize;
pCameraList.Add(camera);
}
public Vector3D WorldSize {
get {
return pWorldSize;
}
set {
pWorldSize = value;
}
}
public void AddCamera(Camera camera) {
pCameraList.Add(camera);
}
public int CameraCount() {
return pCameraList.Count;
}
public void DeleteCamera(int index) {
pCameraList.RemoveAt(index);
}
public Camera GetCamera(int index) {
return pCameraList[index];
}
public void SetCameraFOV(int index, double FOV) {
pCameraList[index].FOV = FOV;
}
public void BeginVectors() {
AddingVectors = true;
}
public void EndVectors() {
AddingVectors = false;
}
public void BeginSegments() {
AddingSegments = true;
}
public void EndSegments() {
AddingSegments = false;
}
public void AddVectors(Vector3D vector) {
pVectors.Add(vector);
}
public void AddSegments(Segment segment) {
pSegments.Add(segment);
}
}
///
/// Default constructor for Vector3D class.
///
public Vector3D() {
}
///
/// Constructor for Vector3D class that takes initial parameters.
///
/// A double point number for the X axis.
/// A double point number for the Y axis.
/// A double point number for the Z axis.
public Vector3D(double x, double y, double z) {
_x = x;
_y = y;
_z = z;
}
public Vector3D(decimal x, decimal y, decimal z) {
_x = (double)x;
_y = (double)y;
_z = (double)z;
}
public Vector3D(int x, int y, int z) {
_x = (double)x;
_y = (double)y;
_z = (double)z;
}
public Point Converted {
get {
if(pCartCoord) {
if(pCenter.IsEmpty) {
throw new Exception("Center coordinate not defined. Use Center = new Point(X, Y).");
}
else {
pConverted.X = (int)(X + pCenter.X);
pConverted.Y = (int)((Y * -1) + pCenter.Y);
}
}
if(pScaled) {
pConverted.X = (int)(pConverted.X * pScaleFactor);
pConverted.Y = (int)(pConverted.Y * pScaleFactor);
}
return pConverted;
}
}
public static bool UseScaling {
set {
pScaled = value;
}
}
public static bool UseCartCoordSystem {
set {
pCartCoord = value;
}
}
public static Point Center {
set {
pCenter = value;
}
}
public static double ScaleFactor {
set {
pScaleFactor = value;
}
}
public static Size ScreenSize {
get {
return pScreenSize;
}
set {
pScreenSize = value;
}
}
public static Vector3D WorldCenter {
get {
return new Vector3D(0, 0, 0);
}
}
///
/// Gets or sets the double point number for the X axis.
///
public double X {
get {
return _x;
}
set {
_x = value;
}
}
///
/// Gets or sets the double point number for the Y axis.
///
public double Y {
get {
return _y;
}
set {
_y = value;
}
}
///
/// Gets or sets the double point number for the Z axis.
///
public double Z {
get {
return _z;
}
set {
_z = value;
}
}
///
/// Adds two vectors together.
///
/// First vector.
/// Second vector.
///
public static Vector3D operator +(Vector3D vector1, Vector3D vector2) {
return new Vector3D {
X = vector1.X + vector2.X,
Y = vector1.Y + vector2.Y,
Z = vector1.Z + vector2.Z
};
}
///
/// Adds a vector to the current one.
///
/// Second vector.
///
public Vector3D Add(Vector3D vector2) {
Vector3D vector1 = this;
return vector1 + vector2;
}
///
/// Subracts two vectors from each other.
///
/// First vector.
/// Second vector.
///
public static Vector3D operator -(Vector3D vector1, Vector3D vector2) {
return new Vector3D {
X = vector1.X - vector2.X,
Y = vector1.Y - vector2.Y,
Z = vector1.Z - vector2.Z
};
}
///
/// Subracts a vector from the current one.
///
/// Second vector.
///
public Vector3D Subtract(Vector3D vector2) {
Vector3D vector1 = this;
return vector1 - vector2;
}
///
/// Multiplies a vector by a scalar.
///
/// Vector to multiply.
/// A double point scalar number.
///
public static Vector3D operator *(Vector3D vector, double scalar) {
return new Vector3D {
X = vector.X * scalar,
Y = vector.Y * scalar,
Z = vector.Z * scalar
};
}
///
/// Multiplies a scalar to the current vector.
///
/// A double point scalar number.
///
public Vector3D Multiply(double scalar) {
Vector3D vector1 = this;
return vector1 * scalar;
}
///
/// Multiplies a vector by a scalar.
///
/// A double point scalar number.
/// Vector to multiply.
///
public static Vector3D operator *(double scalar, Vector3D vector) {
return vector * scalar;
}
///
/// Divides a vector by a divisor.
///
/// Vector to divide.
/// A double point divisor number.
///
public static Vector3D operator /(Vector3D vector, double divisor) {
return new Vector3D {
X = vector.X / divisor,
Y = vector.Y / divisor,
Z = vector.Z / divisor
};
}
///
/// Divides a divisor to the current vector.
///
/// A double point divisor number.
///
public Vector3D Divide(double divisor) {
Vector3D vector1 = this;
return vector1 / divisor;
}
///
/// Compares two vectors to determine if they are equal.
///
/// First vector.
/// Second vector.
///
public static bool operator ==(Vector3D vector1, Vector3D vector2) {
return (vector1.X == vector2.X & vector1.Y == vector2.Y & vector1.Z == vector2.Z);
}
///
/// Compares two vectors to determine if they are not equal.
///
/// First vector.
/// Second vector.
///
public static bool operator !=(Vector3D vector1, Vector3D vector2) {
return (vector1.X != vector2.X & vector1.Y != vector2.Y & vector1.Z != vector2.Z);
}
///
/// Compares object to current vector to determine if they are equal.
///
/// Object to compare to.
///
public override bool Equals(object obj) {
//Check for null and compare run-time types.
if(obj == null || GetType() != obj.GetType())
return false;
Vector3D p = (Vector3D)obj;
return (this.X == p.X) && (this.Y == p.Y) && (this.Z == p.Z);
}
///
/// Generates a unique code for current vector.
///
///
public override int GetHashCode() {
return (int)this.X ^ (int)this.Y ^ (int)this.Z;
}
///
/// Converts the vector to an easy to read string.
///
///
public override string ToString() {
return "{ X=" + this.X + ", Y=" + this.Y + ", Z=" + this.Z + " }";
}
///
/// Caluclates the distance seperating two vectors.
///
/// Second vector.
///
public double Distance(Vector3D vector2) {
Vector3D vector1 = this;
double results = Math.Sqrt(Math.Pow(Math.Abs(vector1.X - vector2.X), 2) + Math.Pow(Math.Abs(vector1.Y - vector2.Y), 2) + Math.Pow(Math.Abs(vector1.Z - vector2.Z), 2));
return results;
}
///
/// Calculates the distance seperating two vectors.
///
/// First vector.
/// Second vector.
///
public static double Distance(Vector3D vector1, Vector3D vector2) {
return vector1.Distance(vector2);
}
///
/// Calculates the magnitude, length, norm of a vector.
///
///
public double Magnitude() {
Vector3D vector1 = this;
Vector3D vector2 = new Vector3D {
X = 0,
Y = 0,
Z = 0
};
return vector1.Distance(vector2);
}
///
/// Calculates the magnitude, length, norm of a vector.
///
/// Vector to determine magnitude.
///
public static double Magnitude(Vector3D vector) {
return vector.Magnitude();
}
///
/// Normalizes vector. Determines direction of vector.
///
///
public Vector3D Normalize() {
Vector3D vector1 = this;
return vector1 / vector1.Magnitude();
}
///
/// Normalizes vector. Determines direction of vector.
///
/// Vector to normalize.
///
public static Vector3D Normalize(Vector3D vector) {
return vector.Normalize();
}
///
/// Determines if vector is empty, contains X=0, Y=0.
///
///
public bool IsEmpty() {
Vector3D vector1 = this;
Vector3D vector2 = new Vector3D {
X = 0,
Y = 0,
Z = 0
};
return (vector1 == vector2);
}
///
/// Calculates the dot product of two vectors.
///
/// Second vector.
///
public double DotProduct(Vector3D vector2) {
Vector3D vector1 = this;
double results = (vector1.X * vector2.X) + (vector1.Y * vector2.Y);
return results;
}
///
/// Calculates the dot product of two vectors.
///
/// First vector.
/// Second vector.
///
public static double DotProduct(Vector3D vector1, Vector3D vector2) {
return vector1.DotProduct(vector2);
}
///
/// Converts the 3D vector to a 2D vector. Uses CameraPosition data.
///
///
public Vector2D Project(int cameraIndex, out bool InRegion, out double ZDepth) {
World currentWorld = ActiveWorld;
Camera currentCamera = currentWorld.GetCamera(cameraIndex);
Vector3D vector = this + currentWorld.WorldSize;
Vector2D vectorResult = new Vector2D();
Vector3D cameraPosition = currentCamera.CameraPosition + currentWorld.WorldSize;
Vector3D translation = new Vector3D();
Vector3D theta = new Vector3D(currentCamera.CameraAngle.Y * (Math.PI / 180),
currentCamera.CameraAngle.X * (Math.PI / 180),
currentCamera.CameraAngle.Z * (Math.PI / 180));
Vector3D viewScreen = currentCamera.Projection;
double MinClip = currentWorld.WorldSize.Z * .25;
double MaxClip = currentWorld.WorldSize.Z * 10;
// Extracted from inline algorithms for debugging purposes
double sinX = Math.Sin(theta.X);
double cosX = Math.Cos(theta.X);
double sinY = Math.Sin(theta.Y);
double cosY = Math.Cos(theta.Y);
double sinZ = Math.Sin(theta.Z);
double cosZ = Math.Cos(theta.Z);
// Translation of coordinates: taken from http://en.wikipedia.org/wiki/3D_projection
translation.X = cosY * (sinZ * (vector.Y - cameraPosition.Y) + cosZ * (vector.X - cameraPosition.X)) - sinY * (vector.Z - cameraPosition.Z);
translation.Y = sinX * (cosY * (vector.Z - cameraPosition.Z) + sinY * (sinZ * (vector.Y - cameraPosition.Y) + cosZ * (vector.X - cameraPosition.X))) + cosX * (cosZ * (vector.Y - cameraPosition.Y) - sinZ * (vector.X - cameraPosition.X));
translation.Z = cosX * (cosY * (vector.Z - cameraPosition.Z) + sinY * (sinZ * (vector.Y - cameraPosition.Y) + cosZ * (vector.X - cameraPosition.X))) - sinX * (cosZ * (vector.Y - cameraPosition.Y) - sinZ * (vector.X - cameraPosition.X));
ZDepth = (currentCamera.CameraPosition).Distance(translation);
// Project translated coordinates to 2D screen
// Prevent div by zero
double Zprojection;
if(translation.Z == 0) {
Zprojection = 0;
}
else {
Zprojection = (viewScreen.Z / translation.Z);
}
if(ZDepth > MaxClip | ZDepth < MinClip) {
InRegion = false;
}
else {
InRegion = true;
}
vectorResult.X = (translation.X - viewScreen.X) * Zprojection;
vectorResult.Y = (translation.Y - viewScreen.Y) * Zprojection;
currentWorld.SetCameraFOV(cameraIndex, (2 * Math.Atan(1 / viewScreen.Z)) * (180 / Math.PI));
return vectorResult;
}
public Vector2D Project(int cameraIndex) {
bool InRegion;
double ZDepth;
return this.Project(cameraIndex, out InRegion, out ZDepth);
}
public Vector2D Project(int cameraIndex, out bool InRegion) {
//bool draw;
double ZDepth;
return this.Project(cameraIndex, out InRegion, out ZDepth);
}
public Vector2D Project(int cameraIndex, out double ZDepth) {
bool InRegion;
//double Zprojection;
return this.Project(cameraIndex, out InRegion, out ZDepth);
}
///
/// Converts the 3D vector to a 2D vector. Uses CameraPosition data.
///
/// Vector to convert.
///
public static Vector2D Project(int cameraIndex, Vector3D vector) {
return vector.Project(cameraIndex);
}
/////
///// Converts the 2D vector to a 2D integer point.
/////
/////
//public Point ConvertToPoint() {
// Vector3D vector = this;
// Point results = new Point { X = (int)vector.X, Y = (int)vector.Y };
// return results;
//}
/////
///// Converts the 2D vector to a 2D integer point.
/////
///// Vector to convert.
/////
//public static Point ConvertToPoint(Vector3D vector) {
// return vector.ConvertToPoint();
//}
public Vector3D Translate(Vector3D vector) {
return this.Add(vector);
}
public static Vector3D Translate(Vector3D original, Vector3D vector) {
return original.Add(vector);
}
public static Vector3D RotateX(Vector3D vector, double degrees) {
//Here we use Euler's matrix formula for rotating a 3D point x degrees around the x-axis
//[ a b c ] [ x ] [ x*a + y*b + z*c ]
//[ d e f ] [ y ] = [ x*d + y*e + z*f ]
//[ g h i ] [ z ] [ x*g + y*h + z*i ]
//[ 1 0 0 ]
//[ 0 cos(x) sin(x)]
//[ 0 -sin(x) cos(x)]
double cDegrees = (Math.PI * degrees) / 180.0f; //Convert degrees to radian for .Net Cos/Sin functions
double cosDegrees = Math.Cos(cDegrees);
double sinDegrees = Math.Sin(cDegrees);
double y = (vector.Y * cosDegrees) + (vector.Z * sinDegrees);
double z = (vector.Y * -sinDegrees) + (vector.Z * cosDegrees);
return new Vector3D(vector.X, y, z);
}
public static Vector3D RotateY(Vector3D vector, double degrees) {
//Y-axis
//[ cos(x) 0 sin(x)]
//[ 0 1 0 ]
//[-sin(x) 0 cos(x)]
double cDegrees = (Math.PI * degrees) / 180.0; //Radians
double cosDegrees = Math.Cos(cDegrees);
double sinDegrees = Math.Sin(cDegrees);
double x = (vector.X * cosDegrees) + (vector.Z * sinDegrees);
double z = (vector.X * -sinDegrees) + (vector.Z * cosDegrees);
return new Vector3D(x, vector.Y, z);
}
public static Vector3D RotateZ(Vector3D vector, double degrees) {
//Z-axis
//[ cos(x) sin(x) 0]
//[ -sin(x) cos(x) 0]
//[ 0 0 1]
double cDegrees = (Math.PI * degrees) / 180.0; //Radians
double cosDegrees = Math.Cos(cDegrees);
double sinDegrees = Math.Sin(cDegrees);
double x = (vector.X * cosDegrees) + (vector.Y * sinDegrees);
double y = (vector.X * -sinDegrees) + (vector.Y * cosDegrees);
return new Vector3D(x, y, vector.Z);
}
//public Vector3D rotateDeg(double degrees) {
// return rotateDeg(this, degrees);
//}
//public static Vector3D rotateDeg(Vector3D vector, double degrees) {
// return rotateRads(vector, degToRads(degrees));
//}
//public Vector3D rotateRads(double rads) {
// return rotateRads(this, rads);
//}
//public static Vector3D rotateRads(Vector3D vector, double rads) {
// return new Vector3D(vector.X * Math.Cos(rads) - vector.X * Math.Sin(rads), +vector.X * Math.Sin(rads) + vector.Y * Math.Cos(rads));
//}
//public Vector3D rotateDegOrigin(Vector3D origin, double degrees) {
// return rotateDegOrigin(this, origin, degrees);
//}
//public static Vector3D rotateDegOrigin(Vector3D vector, Vector3D origin, double degrees) {
// return rotateRadsOrigin(vector, origin, degToRads(degrees));
//}
//public Vector3D rotateRadsOrigin(Vector3D origin, double rads) {
// return rotateRadsOrigin(this, origin, rads);
//}
//public static Vector3D rotateRadsOrigin(Vector3D vector, Vector3D origin, double rads) {
// Vector3D xLated = vector - origin;
// Vector3D rotated = rotateRads(xLated, rads);
// xLated = rotated + origin;
// return xLated;
//}
//public static double degToRads(double deg) {
// return deg * (Math.PI / 180);
//}
//public static double radsToDeg(double rads) {
// return rads * (180 / Math.PI);
//}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace TenneySoftware.Vectors {
///
/// A class that represents 2 dimentional double point precision vectors, as well as the math for it.
///
public class Vector2D : Object{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
private Point pConverted = new Point();
private static bool pScaled;
private static bool pCartCoord;
private static Point pCenter = new Point();
private static double pScaleFactor;
private double _x;
private double _y;
///
/// Default constructor for Vector2D class.
///
public Vector2D() {
}
public Vector2D(Point point) {
_x = point.X;
_y = point.Y;
}
///
/// Constructor for Vector2D class that takes initial parameters.
///
/// A double point number for the X axis.
/// A double point number for the Y axis.
public Vector2D(double x, double y) {
_x = x;
_y = y;
}
public Point Converted {
get {
if (pScaled) {
pConverted.X = (int)Math.Ceiling((X * pScaleFactor));
pConverted.Y = (int)Math.Ceiling((Y * pScaleFactor));
}
if(pCartCoord) {
if(pCenter.IsEmpty) {
throw new Exception("Center coordinate not defined. Use Center = new Point(X, Y).");
}
else {
if (pScaled) {
pConverted.X = (int)Math.Ceiling((double)(pConverted.X + pCenter.X));
}
else {
pConverted.X = (int)Math.Ceiling((X + pCenter.X));
}
if(pConverted.X > 2048) {
pConverted.X = 2048;
}
else if(pConverted.X < 0) {
pConverted.X = 0;
}
if (pScaled) {
pConverted.Y = (int)Math.Ceiling(((double)(pConverted.Y * -1) + pCenter.Y));
}
else {
pConverted.Y = (int)Math.Ceiling(((Y * -1) + pCenter.Y));
}
if(pConverted.Y > 2048) {
pConverted.Y = 2048;
}
else if(pConverted.Y < 0) {
pConverted.Y = 0;
}
}
}
return pConverted;
}
}
public static bool UseScaling {
set {
pScaled = value;
}
}
public static bool UseCartCoordSystem {
set {
pCartCoord = value;
}
}
public static Point Center {
set {
pCenter = value;
}
}
public static double ScaleFactor {
set {
pScaleFactor = value;
}
}
///
/// Gets or sets the double point number for the X axis.
///
public double X{
get{
return _x;
}
set{
_x = value;
}
}
///
/// Gets or sets the double point number for the Y axis.
///
public double Y{
get{
return _y;
}
set{
_y = value;
}
}
///
/// Adds two vectors together.
///
/// First vector.
/// Second vector.
///
public static Vector2D operator +(Vector2D vector1, Vector2D vector2) {
return new Vector2D { X = vector1.X + vector2.X, Y = vector1.Y + vector2.Y };
}
///
/// Adds a vector to the current one.
///
/// Second vector.
///
public Vector2D Add(Vector2D vector2) {
Vector2D vector1 = this;
return vector1 + vector2;
}
///
/// Subracts two vectors from each other.
///
/// First vector.
/// Second vector.
///
public static Vector2D operator -(Vector2D vector1, Vector2D vector2) {
return new Vector2D { X = vector1.X - vector2.X, Y = vector1.Y - vector2.Y };
}
///
/// Subracts a vector from the current one.
///
/// Second vector.
///
public Vector2D Subtract(Vector2D vector2) {
Vector2D vector1 = this;
return vector1 - vector2;
}
///
/// Multiplies a vector by a scalar.
///
/// Vector to multiply.
/// A double point scalar number.
///
public static Vector2D operator *(Vector2D vector, double scalar) {
return new Vector2D { X = vector.X * scalar, Y = vector.Y * scalar };
}
///
/// Multiplies a scalar to the current vector.
///
/// A double point scalar number.
///
public Vector2D Multiply(double scalar) {
Vector2D vector1 = this;
return vector1 * scalar;
}
///
/// Multiplies a vector by a scalar.
///
/// A double point scalar number.
/// Vector to multiply.
///
public static Vector2D operator *(double scalar, Vector2D vector) {
return vector * scalar;
}
///
/// Divides a vector by a divisor.
///
/// Vector to divide.
/// A double point divisor number.
///
public static Vector2D operator /(Vector2D vector, double divisor) {
return new Vector2D { X = vector.X / divisor, Y = vector.Y / divisor };
}
///
/// Divides a divisor to the current vector.
///
/// A double point divisor number.
///
public Vector2D Divide(double divisor) {
Vector2D vector1 = this;
return vector1 / divisor;
}
///
/// Compares two vectors to determine if they are equal.
///
/// First vector.
/// Second vector.
///
public static bool operator ==(Vector2D vector1, Vector2D vector2) {
return (vector1.X == vector2.X & vector1.Y == vector2.Y);
}
///
/// Compares two vectors to determine if they are not equal.
///
/// First vector.
/// Second vector.
///
public static bool operator !=(Vector2D vector1, Vector2D vector2) {
return (vector1.X != vector2.X & vector1.Y != vector2.Y);
}
///
/// Compares object to current vector to determine if they are equal.
///
/// Object to compare to.
///
public override bool Equals(object obj) {
//Check for null and compare run-time types.
if(obj == null || GetType() != obj.GetType()) return false;
Vector2D p = (Vector2D)obj;
return (this.X == p.X) && (this.Y == p.Y);
}
///
/// Generates a unique code for current vector.
///
///
public override int GetHashCode() {
return (int)this.X ^ (int)this.Y;
}
///
/// Converts the vector to an easy to read string.
///
///
public override string ToString() {
return "{ X=" + this.X + ", Y=" + this.Y + " }";
}
///
/// Caluclates the distance seperating two vectors.
///
/// Second vector.
///
public double Distance(Vector2D vector2) {
Vector2D vector1 = this;
double results = Math.Sqrt(Math.Pow(Math.Abs(vector1.X - vector2.X), 2) + Math.Pow(Math.Abs(vector1.Y - vector2.Y), 2));
return results;
}
///
/// Calculates the distance seperating two vectors.
///
/// First vector.
/// Second vector.
///
public static double Distance(Vector2D vector1, Vector2D vector2) {
return vector1.Distance(vector2);
}
///
/// Calculates the magnitude, length, norm of a vector.
///
///
public double Magnitude() {
Vector2D vector1 = this;
Vector2D vector2 = new Vector2D { X = 0, Y = 0 };
return vector1.Distance(vector2);
}
///
/// Calculates the magnitude, length, norm of a vector.
///
/// Vector to determine magnitude.
///
public static double Magnitude(Vector2D vector) {
return vector.Magnitude();
}
///
/// Normalizes vector. Determines direction of vector.
///
///
public Vector2D Normalize() {
Vector2D vector1 = this;
return vector1 / vector1.Magnitude();
}
///
/// Normalizes vector. Determines direction of vector.
///
/// Vector to normalize.
///
public static Vector2D Normalize(Vector2D vector) {
return vector.Normalize();
}
///
/// Determines if vector is empty, contains X=0, Y=0.
///
///
public bool IsEmpty() {
Vector2D vector1 = this;
Vector2D vector2 = new Vector2D { X = 0, Y = 0 };
return (vector1 == vector2);
}
///
/// Calculates the dot product of two vectors.
///
/// Second vector.
///
public double DotProduct(Vector2D vector2) {
Vector2D vector1 = this;
double results = (vector1.X * vector2.X) + (vector1.Y * vector2.Y);
return results;
}
///
/// Calculates the dot product of two vectors.
///
/// First vector.
/// Second vector.
///
public static double DotProduct(Vector2D vector1, Vector2D vector2) {
return vector1.DotProduct(vector2);
}
///
/// Converts the 2D vector to a 3D vector. Adds Z = 0.
///
///
public Vector3D ConvertToVector3D() {
Vector2D vector = this;
Vector3D results = new Vector3D { X = vector.X, Y = vector.Y, Z = 0 };
return results;
}
///
/// Converts the 2D vector to a 3D vector. Adds Z = 0.
///
/// Vector to convert.
///
public static Vector3D ConvertToVector3D(Vector2D vector) {
return vector.ConvertToVector3D();
}
///
/// Converts the 2D vector to a 2D integer point.
///
///
public Point ConvertToPoint() {
Vector2D vector = this;
Point results = new Point { X = (int)vector.X, Y = (int)vector.Y };
return results;
}
///
/// Converts the 2D vector to a 2D integer point.
///
/// Vector to convert.
///
public static Point ConvertToPoint(Vector2D vector) {
return vector.ConvertToPoint();
}
public Vector2D Translate(Vector2D vector) {
return this.Add(vector);
}
public static Vector2D Translate(Vector2D original, Vector2D vector) {
return original.Add(vector);
}
public Vector2D rotateDeg(double degrees) {
return rotateDeg(this, degrees);
}
public static Vector2D rotateDeg(Vector2D vector, double degrees) {
return rotateRads(vector, degToRads(degrees));
}
public Vector2D rotateRads(double rads) {
return rotateRads(this, rads);
}
public static Vector2D rotateRads(Vector2D vector, double rads) {
return new Vector2D(vector.X * Math.Cos(rads) - vector.Y * Math.Sin(rads), +vector.X * Math.Sin(rads) + vector.Y * Math.Cos(rads));
}
public Vector2D rotateDegOrigin(Vector2D origin, double degrees) {
return rotateDegOrigin(this, origin, degrees);
}
public static Vector2D rotateDegOrigin(Vector2D vector, Vector2D origin, double degrees) {
return rotateRadsOrigin(vector, origin, degToRads(degrees));
}
public Vector2D rotateRadsOrigin(Vector2D origin, double rads) {
return rotateRadsOrigin(this, origin, rads);
}
public static Vector2D rotateRadsOrigin(Vector2D vector, Vector2D origin, double rads) {
Vector2D xLated = vector - origin;
Vector2D rotated = rotateRads(xLated, rads);
xLated = rotated + origin;
return xLated;
}
public static double degToRads(double deg) {
return deg * (Math.PI / 180);
}
public static double radsToDeg(double rads) {
return rads * (180 / Math.PI);
}
}
}
Usage:
Vector3D.World world;
Vector3D.Camera camera;
Vector2D.Center = new Point(buffersize.Width / 2, buffersize.Height / 2);
Vector2D.UseCartCoordSystem = true;
camera = new Vector3D.Camera();
camera.CameraPosition = new Vector3D(0, 0, -300);
camera.CameraAngle = new Vector3D(0, 0, 0);
camera.Projection = new Vector3D(0, 0, 1000);
world = new Vector3D.World();
world.AddCamera(camera);
Vector3D.ActiveWorld = world;
private void drawBackground3D() {
graphics3D.ClearBackground();
Vector3D axisXmarker = new Vector3D(100, 0, 0);
Vector3D axisYmarker = new Vector3D(0, 100, 0);
Vector3D axisZmarker = new Vector3D(0, 0, 100);
Vector3D axisCenter = new Vector3D(0, 0, 0);
surface3D.DrawLine(new Pen(Color.Green), axisCenter.Project(0).Converted, axisXmarker.Project(0).Converted);
surface3D.DrawLine(new Pen(Color.Red), axisCenter.Project(0).Converted, axisYmarker.Project(0).Converted);
surface3D.DrawLine(new Pen(Color.Blue), axisCenter.Project(0).Converted, axisZmarker.Project(0).Converted);
}
Let me know if this helps....
carlosPosted Dec 21, 2008, 7:24 PM
Hi!
Thanks for posting the 1st part of the code. I was looking for something like that and i guess i was achieving the same wrong results as you were. So if you can, please post the solution to the problem with that WorldSize trick.
Thank you.
Carlos
Robert TenneyPosted Jun 20, 2008, 10:05 AM
Thank you.