VB - Rotate line about a point

若如初见. 提交于 2020-01-06 11:16:39

问题


I need to rotate an horizontal line about its end point in VB Excel. I tried various solution found on internet but they don't work well.

I give you some images about what I want to do:

This is my line:
point A (72; 378)
point B (165; 378)

And I want to rotate the point A about the point B, the angle of rotation is variable. For example this is a 60 degrees rotation

The letters A and B were added after the screenshot with a photo editor


回答1:


Try this.

Sub test()
    Dim x As Single, y As Single
    Dim nx As Single, ny As Single, l As Single
    Dim i As Single, ra As Single
    Dim Ws As Worksheet
    Dim shp As Shape

    Set Ws = ActiveSheet
    For Each shp In Ws.Shapes
        If shp.Type = msoLine Then
            shp.Delete
        End If
    Next
    x = 165
    y = 378
    l = 165 - 72
    For i = 90 To 150
        ra = WorksheetFunction.Radians(i)
        nx = x - Sin(ra) * l
        ny = y + Cos(ra) * l
        Set shp = Ws.Shapes.AddLine(x, y, nx, ny)
        With shp
            .Line.EndArrowheadStyle = msoArrowheadTriangle
            .Line.ForeColor.RGB = RGB(255, 0, 0)
            .Line.Weight = 2
        End With
        DoEvents
        Application.Wait Now + (TimeSerial(0, 0, 1) / 2)
        shp.Delete

    Next i
    Set shp = Ws.Shapes.AddLine(x, y, nx, ny)
    With shp
        .Line.EndArrowheadStyle = msoArrowheadTriangle
        .Line.ForeColor.RGB = RGB(255, 0, 0)
        .Line.Weight = 2
    End With
End Sub


来源:https://stackoverflow.com/questions/52908312/vb-rotate-line-about-a-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!