VB - Rotate line about a point

2019-08-27 09:34发布

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)

Line to rotate

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

Example of rotation

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

1条回答
Viruses.
2楼-- · 2019-08-27 10:13

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
查看更多
登录 后发表回答