乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 110|回复: 14

[编程交流] can't get coordinates str

[复制链接]

2

主题

4

帖子

2

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 22:06:44 | 显示全部楼层 |阅读模式
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
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 22:12:47 | 显示全部楼层
 
The Y angle between two coordinates is simply the angle between same + (0.5 * Pi), no?
 
  1.        public double GiveAngle(Point2d pt1, Point2d pt2)        // C# code       {           return pt1.GetVectorTo(pt2).Angle + (0.5 * Math.PI);       }
 
 
 
[Edit] - Separately, please use [code ] Tags.
回复

使用道具 举报

29

主题

519

帖子

477

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
163
发表于 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.
 
Your  logic is a bit off and will not deliver correct answers. You need to  consider which quadrant you are in and the special cases when the angle  is due E, N , W or S. The resulting code can get a little complicated so  I would recommend another approach. You have the starting point and end  point 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:
 
  1.    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.Delete
I hope that helps.
 
@ BlackBox:
Nice neat and concise code. Unfortunately it doesn't work in VBA, as far as I am aware.
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 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
回复

使用道具 举报

29

主题

519

帖子

477

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
163
发表于 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:
 
  1.                Xdist = Abs(x2 - x1): Ydist = Abs(y2 - y1)       ATAN3 = Math.Atn(Ydist / Xdist)       dAngle = ATAN3 * 180 / PI
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 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?
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 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
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 22:37:41 | 显示全部楼层
If VBA, why not use:
 
  1. ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)
 
 
[Edit] - ... + (0.5 * Pi), of course.
回复

使用道具 举报

29

主题

519

帖子

477

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
163
发表于 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.
回复

使用道具 举报

29

主题

519

帖子

477

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
163
发表于 2022-7-6 22:48:04 | 显示全部楼层
 
 
Why not indeed , but would you need to have:
  1. (0.5 * Pi) - ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)
ie 90° minus angle from X-Axis?
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 11:33 , Processed in 0.328097 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表