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 in Others 66 views

## 1 answer to this question.

Use this:

```Option Explicit
Type ObjLocData
X As Long
Y As Long
End Type

Dim RotObj As Shape
Dim RotLoc1 As ObjLocData
Dim RotLoc2 As ObjLocData
Dim AxisObj As Shape
Dim AxisLoc As ObjLocData
Dim Pi As Double

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.
```
• 59,740 points

## SQL - The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

I ran into the same problem due ...READ MORE

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

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

## 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

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

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

## Retrieve epay.info Balance with VBA and Excel

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

## How to load file to Excel Power query from SFTP site

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

## Using VBA Excel to create a gramatically correct list

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