.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to obtain the angle between two vector3d? The angle 's range should be 0-2PI

14 REPLIES 14
Reply
Message 1 of 15
waterharbin
5075 Views, 14 Replies

How to obtain the angle between two vector3d? The angle 's range should be 0-2PI

Hello,I define a vextor via two points(ptStart and ptDirection).And now I want to get the angle between my vector3d and the X Axis.Here is my code:

Dim Ang As Double = ptStart.GetVectorTo(ptDirection).GetAngleTo(Vector3d.XAxis)

But the range is 0-PI.Smiley SadHow can I change it to 0-2PI?

14 REPLIES 14
Message 2 of 15

Hi,

 

   I see there is another version of this method:

 

      Vector3d.GetAngleTo Method (Vector3d, Vector3d)

      Returns the angle between this vector and the vector vector in the range [0, 2 x Pi]. 

 

   Please, find its description in the Managed Class Reference Guide (\ObjectARX 2012\docs\arxmgd.chm).



Marat Mirgaleev
Developer Technical Services
Autodesk Developer Network
Message 3 of 15
BabylonLion
in reply to: waterharbin

Hi! Try this.

 

double Ang = ptStart.GetVectorTo(ptDirection).GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis.Negate());

 

Message 4 of 15
waterharbin
in reply to: BabylonLion

Nice code.haha.

Message 5 of 15
adadnet
in reply to: Marat.Mirgaleev

hi marat

 

although i do not want to spoil the party, i desperately fail to obtain any angles above the 0-pi interval. i admit i still use autocad 2011, however, the documentation for the .getangleto(vec; vecref) appears as you state.

 

considering the problem, i could do myself with some clarification as to how it's supposed to work:

 

starting with a vector3d, i look for the angle to another vector3d. to keep things simple, let's say they both originate in the same point and 'point away' from it in separate directions. why is there an optional reference-vector3d? if it's only to expand the interval from 0-pi to 0-2*pi, then an optional boolean would have been satisfactory.

 

many thanks, felix

Message 6 of 15
adadnet
in reply to: adadnet

yes of course, as soon as i posted it...

 

to whom it may concern

 

the additional vecref can be used to define [or fixate] that one dimension of the three (in 3d) that leaves the other two to span the plane in which the angle is measured. in turn it means that the option without the vecref leaves it completely to the two original vectors, i.e. the calling one and the parameter one, to define a plane and the angle within, which also explains why then with the information given it is not possible to give angles above pi, because 'they technically then do not exist'.

 

hope you enjoyed the show

 

felix

 

Message 7 of 15
5thSth
in reply to: adadnet

hello. Sorry for ressurecting a thread, but I have an issue using the "getAngle".

the problem is the angle I'm getting is nowhere in the PI range

 

all the "input numbers" turn out as expected.

 

here's my code:

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    ObjectId id = res.ObjectId;
                    Point3d picked = res.PickedPoint;
                    var pline = tr.GetObject(id, OpenMode.ForRead) as Polyline;

                    Point3d onPlinePt = pline.GetClosestPointTo(picked, ed.GetCurrentView().ViewDirection, false);
                    Line3d perpLine = new Line3d(picked, onPlinePt);

                    Vector3d xAxis = new Vector3d(1,0,0);
                    Vector3d perpVector = onPlinePt-picked;

                    double myAngleRad = perpVector.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis.Negate());
                    double myAngle = myAngleRad * 180 / Math.PI;

}

and here's the result:

angles.jpg

 

 

any ideas what I'm doing wrong?

Message 8 of 15
_gile
in reply to: 5thSth

Hi,

 

The angle you get is in the 0 2pi range.

1.0094597E-188 (scientific notation) is just very closed to 0.0.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 15
5thSth
in reply to: _gile

true, it was por choice of expression. 
my question stands, since I've been getting numbers 10e-188 no matter if I chose vertical, horizontal, or @ 45 deg objects

 

4 example, this is the result for a completely horizontalline - vector (1,0,0). 

the angle to Y axis should be 90deg or PI/2 (1,57)

instead, I get this results...

 

angle.jpg

Message 10 of 15
_gile
in reply to: 5thSth


5thSth a écrit :

4 example, this is the result for a completely horizontalline - vector (1,0,0). 

the angle to Y axis should be 90deg or PI/2 (1,57)



 

 

No, internally (and programmatically) AutoCAD always measures angles in radians, CCW and starting from East.

If you have different settings (e.g. ANGDIR / ANGBASE), you have to make some calculus to convert the angle or use the Converter.AngleToString() method.

 

try:

AngleToString(perpVector.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis));

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 11 of 15
5thSth
in reply to: _gile

Ive tried it, but "angleToString" method:

 

                    var newAngle = Autodesk.AutoCAD.Runtime.Converter.AngleToString(Vector3d.XAxis.GetAngleTo(Vector3d.YAxis,Vector3d.ZAxis));

                    Application.ShowAlertDialog("Angle between X and Y axis is:" + newAngle);

and result angle for X and Y axis turns out to be "0" 😞

and if I try only to .GetAngleTo(Vector3d.Yaxis)  without Z ... : 314 (which is more of a "Rad" result.

-will keep experimenting; ty

 

 

 

but I'd still like to know what the problem was with other method ;

its not a "rad/angle" conversion problem. I know all about rads, and the results Im getting are nowhere near to expected rad or deg results.

to make the problem more apparent, I've rewritten the code to print the

angle to X and Y axis... which is "90 deg" or "PI/2" in rads which is equal to "1,57"

angle to X and X axis... which is "0,00 deg" or "0,00" in rads which equals to "0,00"

 

                    double      angleResult  = new double();
                    double      angleResult2 = new double();
                    angleResult  = Vector3d.XAxis.GetAngleTo(Vector3d.YAxis);
                    angleResult2 = Vector3d.XAxis.GetAngleTo(Vector3d.XAxis);

                    Application.ShowAlertDialog("axisX vector:" + Vector3d.XAxis.ToString() + 
                                                "\naxisY vector:" + Vector3d.YAxis.ToString() +
                                                "\nAngle axisX to axisY: " + angleResult.ToString()+
                                                "\nAngle axisX to axisX" + angleResult2.ToString());

error.jpg

 

you can tell something is really wrong simply by looking at it... since angle1 and angle2 results turn out to be the same number!!! (-2,4785E +215)

Message 12 of 15
_gile
in reply to: 5thSth

Ok,

 

It seems you really have a problem with your AutoCAD (try to repar or re-install it).

 

By my side, I got correct results:

 

angle.png



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 13 of 15
5thSth
in reply to: _gile

wow! 
Thank you for going through the trouble of testing it.

😞

 

will try to run code on desktop.

 

could be AMD cpu...

Message 14 of 15
5thSth
in reply to: 5thSth

on my desktop I get correct result.

 

but I tried to find where the bug is on laptop...

 

and when I run the dll directly from autocad, it gives correct result.

 

however, if I try to "debug" through visual studio, I get the above mentioned "error" results

Message 15 of 15
5thSth
in reply to: 5thSth

It appears the problem was my windows. I was using windows 10 -Technical Preview

I upgraded to win 10, and the problem is gone (same visual studio and Acad and it works)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost