Skip to content

Commit

Permalink
implement shape rotation partially
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jul 22, 2024
1 parent 80dcd72 commit 197e42a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.github.creme332.controller.canvas.drawing.DrawIrregularPolygon;
import com.github.creme332.controller.canvas.drawing.DrawLine;
import com.github.creme332.controller.canvas.drawing.DrawRegularPolygon;
import com.github.creme332.controller.canvas.transform.Rotator;
import com.github.creme332.controller.canvas.transform.Translator;
import com.github.creme332.model.AppState;
import com.github.creme332.model.CanvasModel;
Expand Down Expand Up @@ -77,6 +78,7 @@ public CanvasController(AppState app, Canvas canvas) {

// initialize other canvas sub-controllers
new Translator(app, canvas);
new Rotator(app, canvas);

// when canvas is resized, update dimensions and reset zoom
canvas.addComponentListener(new ComponentAdapter() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.creme332.controller.canvas.transform;

import java.awt.geom.Point2D;

import com.github.creme332.model.AppState;
import com.github.creme332.model.Mode;
import com.github.creme332.model.ShapeWrapper;
import com.github.creme332.view.Canvas;

public class Rotator extends AbstractTransformer {

public Rotator(AppState app, Canvas canvas) {
super(app, canvas);
}

@Override
public void handleShapeSelection(int shapeIndex) {
// get copy of selected shape
ShapeWrapper selectedWrapperCopy = canvasModel.getShapeManager().getShapes().get(shapeIndex);

// TODO: Get rotation details

// perform rotation
selectedWrapperCopy.rotate(30, new Point2D.Double(0, 0));

// replace old shape with new one
canvasModel.getShapeManager().editShape(shapeIndex, selectedWrapperCopy);

// repaint canvas
canvas.repaint();
}

@Override
public boolean shouldDraw() {
return getCanvasMode() == Mode.ROTATE_ABOUT_POINT;
}

}
41 changes: 41 additions & 0 deletions src/main/java/com/github/creme332/model/ShapeWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,47 @@ public void translate(final Point2D translationVector) {
}
}

/**
* Rotates shape and plotted points clockwise about a specified point.
*
* @param radAngle the angle of rotation in radians
* @param pivot the x-y coordinates of the rotation point
*/
public void rotate(double radAngle, Point2D pivot) {
AffineTransform transform = new AffineTransform();

// Step 1: Translate the shape to the origin (negative of the rotation point)
transform.translate(pivot.getX(), pivot.getY());

// Step 2: Rotate the shape
transform.rotate(radAngle);

// Step 3: Translate the shape back to its original position
transform.translate(-pivot.getX(), -pivot.getY());

final Shape oldShape = shape;
Shape transformedShape;

if (oldShape instanceof Polygon) {
// if selected shape is of type polygon, ensure that the transformed shape is of
// type Polygon as well
transformedShape = PolygonCalculator.transformPolygon((Polygon) oldShape,
transform);
} else {
transformedShape = transform.createTransformedShape(oldShape);
}

// replace old shape with transformed shape
setShape(transformedShape);

// rotate plotted points
for (int i = 0; i < plottedPoints.size(); i++) {
Point2D oldPoint = plottedPoints.get(i);
Point2D rotatedPoint = PolygonCalculator.rotatePointAboutPivot(oldPoint, pivot, radAngle);
plottedPoints.set(i, rotatedPoint);
}
}

public Shape getShape() {
return shape;
}
Expand Down

0 comments on commit 197e42a

Please sign in to comment.