Find right- and leftmost 2d point of a horizontal circle in 3d Vector environment
I'm drawing a 3D pie chart that is calculated with in 3D vectors, projected to 2D vectors and then drawn on a Graphics object. I want to calculate the most left and right point of the circle after projected in 2d. (So not 0 and 1 Pi!) The method to create a vector, draw and project to a 2d vector are below. Any开发者_JS百科one knows the answer?
public class Vector3d
{
public var x:Number;
public var y:Number;
public var z:Number;
//the angle that the 3D is viewed in tele or wide angle.
public static var viewDist:Number = 700;
function Vector3d(x:Number, y:Number, z:Number){
this.x = x;
this.y = y;
this.z = z;
}
public function project2DNew():Vector
{
var p:Number = getPerspective();
return new Vector(p * x, p * y);
}
public function getPerspective():Number{
return viewDist / (this.z + viewDist);
}
}
It looks like you posted your 3d perspective transform. The simplest is if the circle isn't rotated:
class Circle
{
public var radius:Number;
public var position:Vector3d;
public function Circle(_radius:Number, _position:Vector3d)
{
radius = _radius;
position = _position;
}
}
var circ:Circle = new Circle(100, new Vector3d(1, 2, 3));
var leftMost:Vector3d = new Vector3d(circ.x - circ.radius, circ.y);
var rightMost:Vector3d = new Vector3d(circ.y + circ.radius, circ.y);
If you rotate the circle along the y-axis then the points would need to be rotated around the same axis by the same amount. That is, if you have a transform matrix that applies to the circle, just apply the same transform matrix to the leftMost and rightMost vectors as well. Have a look at Matrix3D and it's various rotation methods.
class Circle
{
public var radius:Number;
public var position:Vector3d;
public var transform:Matrix3d;
public function Circle(_radius:Number, _position:Vector3d)
{
radius = _radius;
position = _position;
}
public function getLeftMostProjected():Vector
{
var vec:Vector3d = new Vector3d(position.x - radius, position.y, position.z);
vec = transform.transformVector(vec);
return vec.project2DNew();
}
public function getRightMostProjected():Vector
{
var vec:Vector3d = new Vector3d(position.x + radius, position.y, position.z);
vec = transform.transformVector(vec);
return vec.project2DNew();
}
}
精彩评论