明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1866|回复: 1

x,y绕x0,y0任意角度算法

[复制链接]
发表于 2008-9-11 21:29 | 显示全部楼层 |阅读模式

要求:

  中心点为x0,y0.求x,y绕x0,y0旋转任意角度的坐标

  方程

  设旋转角度为A,(x,y)变换之后的坐标为(x',y'),那么:
  x'=(x-x0)*cosA-(y-y0)*sinA+x0
  y'=(x-x0)*sinA+(y-y0)*cosA+y0

程序

 
Sub ls()
  Dim P0(0 To 2) As Double, pp(0 To 2) As Double
  P0(0) = 0: P0(1) = 0
  x = 10: y = 0
  Alfa = 30 * Pi / 180
  pp(0) = (x - P0(0)) * Cos(Alfa) - (y - P0(1)) * Sin(Alfa) + P0(0)
  pp(1) = (x - P0(0)) * Sin(Alfa) + (y - P0(1)) * Cos(Alfa) + P0(1)
  Dim objLine As AcadLine
  Set objLine = ThisDrawing.ModelSpace.AddLine(pp, P0)

End Sub
Function Pi()
  Pi = 4 * Atn(1)
End Function

 

 楼主| 发表于 2008-9-12 10:26 | 显示全部楼层
本帖最后由 作者 于 2008-9-16 20:45:50 编辑

带功能函数

Type ReturnPoint
  X As Double
  Y As Double
  Z As Double
End Type

Sub lss()
  Dim p2(0 To 2) As Double, p1(0 To 2) As Double
  Dim objLine As AcadLine

  Dim pXY As Variant, P0 As Variant
  Dim pp As ReturnPoint
  pXY = Array(2, 2, 0)
  P0 = Array(0, 0, 0)
 
  pp = RotatingAroundTheZAxis(P0, pXY, 30)
  For ii = 0 To 2
    p1(ii) = P0(ii)
  Next ii
  p2(0) = pp.X: p2(1) = pp.Y: p2(2) = pp.Z
  Set objLine = ThisDrawing.ModelSpace.AddLine(p1, p2)
''
  pYZ = Array(0, 2, 2)
  P0 = Array(0, 0, 0)
 
  pp = RotatingAroundTheXAxis(P0, pYZ, 30)
  For ii = 0 To 2
    p1(ii) = P0(ii)
  Next ii
  p2(0) = pp.X: p2(1) = pp.Y: p2(2) = pp.Z
  Set objLine = ThisDrawing.ModelSpace.AddLine(p1, p2)
''
  pYZ = Array(2, 0, 2)
  P0 = Array(0, 0, 0)
 
  pp = RotatingAroundTheYAxis(P0, pYZ, 30)
  For ii = 0 To 2
    p1(ii) = P0(ii)
  Next ii
  p2(0) = pp.X: p2(1) = pp.Y: p2(2) = pp.Z
  Set objLine = ThisDrawing.ModelSpace.AddLine(p1, p2)

End Sub

Function RotatingAroundTheXAxis(P0 As Variant, pXY As Variant, Gama As Double) As ReturnPoint
  zz = pXY(2): yy = pXY(1)
  z0 = P0(2): y0 = P0(1)
  Gama = Gama * Pi / 180
  With RotatingAroundTheXAxis
    .X = pXY(0)
    .Y = (yy - y0) * Cos(Gama) - (zz - z0) * Sin(Gama) + y0
    .Z = (yy - y0) * Sin(Gama) + (zz - z0) * Cos(Gama) + z0
  End With
End Function

Function RotatingAroundTheYAxis(P0 As Variant, pXY As Variant, Belta As Double) As ReturnPoint
  xx = pXY(0): zz = pXY(2)
  x0 = P0(0): z0 = P0(2)
  Belta = Belta * Pi / 180
  With RotatingAroundTheYAxis
 
    .X = (xx - x0) * Cos(Belta) - (zz - z0) * Sin(Belta) + x0
    .Y = pXY(1)
    .Z = (xx - x0) * Sin(Belta) + (zz - z0) * Cos(Belta) + z0
   
  End With
End Function

Function RotatingAroundTheZAxis(P0 As Variant, pXY As Variant, Alfa As Double) As ReturnPoint
  xx = pXY(0): yy = pXY(1)
  x0 = P0(0): y0 = P0(1)
  Alfa = Alfa * Pi / 180
  With RotatingAroundTheZAxis
    .X = (xx - x0) * Cos(Alfa) - (yy - y0) * Sin(Alfa) + x0
    .Y = (xx - x0) * Sin(Alfa) + (yy - y0) * Cos(Alfa) + y0
    .Z = pXY(2)
  End With
End Function


Function Pi()
  Pi = 4 * Atn(1)
End Function


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

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-6-29 11:06 , Processed in 0.144040 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表