guusdehart 发表于 2022-7-6 22:06:44

can't get coordinates str

I have set in autocad the correct angle settings, have been applying 20 different functions to obtain the correct angle (Y axis) between two coordinates, but I keep getting strange angles back from this routine. Either the correct angle is difference with 90, or 450. Any idea why? I have gone through most of math forums on the internet. I need the angle correctly in order to scale a polygon.
thanks.
 
Public Function GiveAngle(DegRad As Boolean, x1 As Double, y1 As Double, x2 As Double, y2 As Double) As Double
Dim Xdist, Ydist As Double
Dim ATAN3 As Double
Dim PI As Double
PI = 3.14159265358979
Xdist = (x2 - x1)
Ydist = (y2 - y1)
 
If Abs(Ydist) > Abs(Xdist) Then
    If Ydist > 0 Then
       ATAN3 = Math.Atn((x2 - x1) / (y1 - y2))
    Else
      ATAN3 = Math.Atn((x2 - x1) / (y1 - y2)) + PI
    End If
Else
    If Xdist > 0 Then
      ATAN3 = 0.5 * PI - Atn((x2 - x1) / (y1 - y2))
    Else
      ATAN3 = -0.5 * PI - Atn((x2 - x1) / (y1 - y2))
    End If
End If
    GiveAngle = ATAN3 * 180 / PI
 
 
End Function

BlackBox 发表于 2022-7-6 22:12:47

 
The Y angle between two coordinates is simply the angle between same + (0.5 * Pi), no?
 

       public double GiveAngle(Point2d pt1, Point2d pt2)      // C# code       {         return pt1.GetVectorTo(pt2).Angle + (0.5 * Math.PI);       }
 
 
 
- Separately, please use Tags.

Tyke 发表于 2022-7-6 22:15:38

First of all read up on how you post your code or you will get a moderator onto you.
 
Yourlogic is a bit off and will not deliver correct answers. You need toconsider which quadrant you are in and the special cases when the angleis due E, N , W or S. The resulting code can get a little complicated soI would recommend another approach. You have the starting point and endpoint coordinates, so use them to draw a temporary line, interrogate the line to get its angle and delete the temporary line.
 
This code should work for you if you adapt it to your situation:
 

   Dim basePnt As Variant   Dim returnPnt As Variant   Dim DistLine As AcadLine   Dim Bearing As Double   Dim AngleXY As Double   Dim PI As Double      ' Set the value for PI   PI = Math.Atn(1#) * 4      ' select points on screen   basePnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Select first point...")   returnPnt = ThisDrawing.Utility.GetPoint(basePnt, "Select second point...")      ' Create a line from the base point and the last point entered   Dim lineObj As AcadLine   Set DistLine = ThisDrawing.ModelSpace.AddLine(basePnt, returnPnt)      ' Get angle of line (rads) and convert it to degrees   AngleXY = DistLine.Angle   Bearing = AngleXY * 180# / PI      ' Format the result   Bearing = Format(Bearing, "##0.00000")      ' Display result in a message box   MsgBox "Angle from system zero =   " & Bearing & "°" _         , vbOKOnly, "3D-Angles"      ' Delete temporary line   DistLine.DeleteI hope that helps.
 
@ BlackBox:
Nice neat and concise code. Unfortunately it doesn't work in VBA, as far as I am aware.

BlackBox 发表于 2022-7-6 22:20:43

Firstly, I'm having a _really_ rough Monday morning, even for a Monday morning... So kindly educate me, where I must be overlooking the obvious....
 
 
Given the OP's request to derive Y angle between two coordinates, I am not sure I understand the relevance of quadrant (tabling the line aspect for now)... The X angle is always the angle between start point and end point, thus Y angle will always be X angle + (0.5 * Pi), no?
 
Cheers

Tyke 发表于 2022-7-6 22:23:59

 
Sorry to hear that your start to the week is not going too well, my Monday is over now and I'm off to enjoy a nice red wine with my little lady .
 
You could be right with your assumption that it is just a simple matter of finding the acute angle to the X axis and if that is so my solution is not what the OP is looking for. But this code will give him the answer that he's after:
 

               Xdist = Abs(x2 - x1): Ydist = Abs(y2 - y1)       ATAN3 = Math.Atn(Ydist / Xdist)       dAngle = ATAN3 * 180 / PI

BlackBox 发表于 2022-7-6 22:28:26

 
Me too!It has nothing to do with CAD either; I'll reserve comment as it is off topic. Nonetheless, thanks for the well wishes, and I hope you and the captivating Mrs. Tyke enjoy your vino.
 
 
 
 
That is a nice demonstration of Pythagorean theorem, but is essentially a verbose version of the GetVectorTo() Method, no?

BlackBox 发表于 2022-7-6 22:33:51

I just saw this:
 
 
Ohhhh... This is VBA? I thought the OP posted VB.NET (hence the C# reply)!
 
These languages really need to be split up into their own forums for clarity of those seeking help, and those who might offer it.
 
Cheers

BlackBox 发表于 2022-7-6 22:37:41

If VBA, why not use:
 

ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)
 
 
- ... + (0.5 * Pi), of course.

Tyke 发表于 2022-7-6 22:42:27

 
Yes it is verbose, but unfortunately VBA is not as concise as VLISP or C#. But on the other hand, much more concise than the OP's original code.
 
Let's wait and see what the OP says, perhaps we have both missed his point.

Tyke 发表于 2022-7-6 22:48:04

 
 
Why not indeed , but would you need to have:

(0.5 * Pi) - ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)ie 90° minus angle from X-Axis?
页: [1] 2
查看完整版本: can't get coordinates str