Skip to content

Commit

Permalink
implement draw ellipse
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jun 21, 2024
1 parent 4c69099 commit b0ef5f8
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/main/java/com/github/creme332/controller/CanvasController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.awt.image.BufferedImage;

import com.github.creme332.algorithms.CircleCalculator;
import com.github.creme332.algorithms.EllipseCalculator;
import com.github.creme332.model.AppState;
import com.github.creme332.model.CanvasModel;
import com.github.creme332.model.Mode;
Expand All @@ -32,6 +33,7 @@ public class CanvasController implements PropertyChangeListener {
private Canvas canvas;

private CircleCalculator circleCalculator = new CircleCalculator();
private EllipseCalculator ellipseCalculator = new EllipseCalculator();
/**
* Used to store coordinate where mouse drag started
*/
Expand Down Expand Up @@ -101,7 +103,25 @@ public void mouseMoved(MouseEvent e) {
currentWrapper.setShape(getCircle((int) center.getX(), (int) center.getY(), roundedRadius));
canvas.repaint();
return;
}

if (app.getMode() == Mode.DRAW_ELLIPSE && currentWrapper != null
&& currentWrapper.getPlottedPoints().size() == 2) {
// create an ellipse
Point2D center = currentWrapper.getPlottedPoints().get(0);
int ry = (int) Math.abs(currentWrapper.getPlottedPoints().get(1).distance(center));
int rx = (int) Math.abs(polySpaceMousePosition.distance(center));

if (rx == 0 || ry == 0) {
return;
}

int[][] coordinates = ellipseCalculator.getOrderedPoints((int) center.getX(), (int) center.getY(),
rx, ry);
Polygon ellipse = new Polygon(coordinates[0], coordinates[1], coordinates[0].length);
currentWrapper.setShape(ellipse);
canvas.repaint();
return;
}

}
Expand Down Expand Up @@ -235,7 +255,7 @@ private void handleMousePressed(MouseEvent e) {

if (app.getMode() == Mode.DRAW_ELLIPSE) {
if (currentWrapper == null) {
// first foci has been selected
// center of ellipse has been selected

// create a shape wrapper
currentWrapper = new ShapeWrapper(model.getFillColor(), model.getFillColor(), model.getLineType(),
Expand All @@ -246,15 +266,25 @@ private void handleMousePressed(MouseEvent e) {
model.getShapes().add(currentWrapper);

} else {
// second foci has now been selected
if (currentWrapper.getPlottedPoints().size() == 1) {
// second point has now been selected
currentWrapper.getPlottedPoints().add(polySpaceMousePosition);
} else {
// third point has been selected
currentWrapper.getPlottedPoints().add(polySpaceMousePosition);

// create an ellipse
Polygon ellipse = new Polygon();
Point2D center = currentWrapper.getPlottedPoints().get(0);
int ry = (int) Math.abs(currentWrapper.getPlottedPoints().get(1).distance(center));
int rx = (int) Math.abs(currentWrapper.getPlottedPoints().get(2).distance(center));

if (rx == 0 || ry == 0) {
return;
}

int[][] coordinates = ellipseCalculator.getOrderedPoints((int) center.getX(), (int) center.getY(),
rx, ry);
Polygon ellipse = new Polygon(coordinates[0], coordinates[1], coordinates[0].length);

currentWrapper.setShape(ellipse);

Expand Down

0 comments on commit b0ef5f8

Please sign in to comment.