views:

97

answers:

1

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. Anyone 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);
    }
}
+1  A: 

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();
    }
}
James Fassett
Thanks for the comprehensive answer, but I think I have not clearly explained what I meant. With most right and left point I meant the point that after the 2D projection is most right and left visually. (So the x of the 2d vector) That is not the same as 0 and 1 Pi in a 3d circle with 3d vectors. I first thought that too and it is a pity that I can't add any image. But if you're in a CAD program, and look from the top to a horizontal circle, you will see that the leftmost and rightmost point is not 0 and 1 is Pi in 3d Vectors.
Olivier de Jonge