2D rotation of an object along a custom axis

0 votes

I'm attempting to figure out how to rotate a 2D object along any axis, which could be outside of the shape itself or somewhere else.

Any pseudocode will do; I just happen to use the VBA programming language for this.

I am looking for a function similar to this:

function rotate(obj, axisX, axisY, angle)
    obj.rotate(angle)
    obj.x = ?
    obj.y = ?

I'm not sure if it matters, but let's suppose it does for the sake of simplicity that the shape is a rectangle.

And when the shape has been rotated, the x, y coordinates would be those of a square containing the rotated shape.

Jan 31, 2023 in Others by Kithuzzz
• 38,000 points
376 views

1 answer to this question.

0 votes

Use this:

Option Explicit
Type ObjLocData
    X As Long
    Y As Long
End Type
Sub Rotate_Object_About_Axis()

    Dim RotObj As Shape
    Dim RotLoc1 As ObjLocData
    Dim RotLoc2 As ObjLocData
    Dim AxisObj As Shape
    Dim AxisLoc As ObjLocData
    Dim R_Rad As Double
    Dim Pi As Double
    
    R_Rad = 0.1
    Pi = 3.14159
    
    Set AxisObj = ActiveSheet.Shapes("AxisObj")
    AxisLoc = ObjectLocation(AxisObj)
    Set RotObj = ActiveSheet.Shapes("RotObj")
    RotLoc1 = ObjectLocation(RotObj)
    Debug.Print AxisLoc.X
    
    RotLoc2 = RotateCoordinates(AxisLoc.X, AxisLoc.Y, RotLoc1.X, RotLoc1.Y, R_Rad)
    With RotObj
        .Left = RotLoc2.X - (.Width / 2)
        .Top = RotLoc2.Y - (.Height / 2)
        .Rotation = .Rotation + (R_Rad * 360 / (2 * Pi))
    End With
    
End Sub
Function ObjectLocation(Shp As Object) As ObjLocData
    ObjectLocation.X = Shp.Left + (Shp.Width / 2)
    ObjectLocation.Y = Shp.Top + (Shp.Height / 2)
End Function
Function RotateCoordinates(Xa, Ya, X, Y, R) As ObjLocData
    RotateCoordinates.X = ((X - Xa) * Cos(R)) - ((Y - Ya) * Sin(R)) + Xa
    RotateCoordinates.Y = ((X - Xa) * Sin(R)) + ((Y - Ya) * Cos(R)) + Ya
End Function
'Location Formulas
'X'=(x-p)cos(?)-(y-q)sin(?)+p,
'y'=(x-p)sin(?)+(y-q)cos(?)+q.

Example:
enter image description here
enter image description here
enter image description here

answered Jan 31, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
0 answers

how to list the contents of a asset into an event

May 29, 2019 in Others by anonymous
661 views
0 votes
1 answer
0 votes
1 answer

Can R do the equivalent of an HLOOKUP nested within a VLOOKUP?

When working in a program like R, ...READ MORE

answered Nov 11, 2022 in Others by narikkadan
• 63,600 points
798 views
0 votes
1 answer

How can I store the data of an open excel workbook in a collection using BluePrism?

To do what you want is like ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,600 points
1,362 views
0 votes
1 answer

Cross sum from a number but cross sum should total to one digit

Try this: Option Explicit Sub Quersumme_OneDigit() ...READ MORE

answered Mar 17, 2023 in Others by Kithuzzz
• 38,000 points
465 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,212 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,663 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
908 views
0 votes
1 answer

Is there any way in python to auto-correct spelling mistake in multiple rows of an excel files of a single column?

Use Spellchecker for doing your stuff: import pandas ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,600 points
2,054 views
0 votes
1 answer

How to stick an embedded document in a specific cell of an excel

Solution Select the documents (you can use the ...READ MORE

answered Oct 18, 2022 in Others by narikkadan
• 63,600 points
703 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP