From bbe7cbee8f2da75da00280dd9ac0c42a7bdbd1cf Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Mon, 27 Mar 2017 14:26:38 +1300 Subject: [PATCH] Use canvas polygon to draw a triangle #story[480] --- .../seng302/controllers/CanvasController.java | 53 ++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index c3920c7d..e656039e 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -27,9 +27,7 @@ import seng302.models.mark.SingleMark; import seng302.models.parsers.ConfigParser; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; +import java.util.*; /** * Created by ptg19 on 15/03/17. @@ -64,6 +62,9 @@ public class CanvasController { private boolean annotationCheck = true; + ///test + private HashSet headingTest = new HashSet<>(); + /** * Initialize the controller */ @@ -279,15 +280,13 @@ public class CanvasController { */ private void drawWake(GraphicsContext gc, double x, double y, double speed, Color color, double heading){ double angle = Math.toRadians(heading); - speed = speed * 0.50; // Half the size of the wake - - double newX = x + speed * Math.cos(angle);//(nextX * Math.cos(angle) - nextY * Math.sin(angle)) * length; - double newY = y + speed * Math.sin(angle);//(nextX * Math.sin(angle) + nextY * Math.cos(angle)) * length; + speed = speed * 0.5; // Half the size of the wake + Point newP = new Point(0, speed); + newP.rotate(angle); gc.setStroke(color); gc.setLineWidth(1.0); - - gc.strokeLine(x+5, y+5, newX, newY); + gc.strokeLine(x, y, newP.x + x, newP.y + y); } /** @@ -304,8 +303,6 @@ public class CanvasController { double x = (lon - ORIGIN_LON) * SCALE; double y = (ORIGIN_LAT - lat) * SCALE; - double diameter = 9; - gc.setFill(color); if (annotationCheck) { @@ -314,14 +311,46 @@ public class CanvasController { gc.setLineWidth(3); gc.fillText(name + ", " + speed + " knots", x + 15, y + 15); } +// double diameter = 9; +// gc.fillOval(x, y, diameter, diameter); + double angle = Math.toRadians(heading); - gc.fillOval(x, y, diameter, diameter); + Point p1 = new Point(0, -15); // apex point + Point p2 = new Point(7, 4); // base point + Point p3 = new Point(-7, 4); // base point + p1.rotate(angle); + p2.rotate(angle); + p3.rotate(angle); + double[] xx = new double[] {p1.x + x, p2.x + x, x, p3.x + x}; + double[] yy = new double[] {p1.y + y, p2.y + y, y, p3.y + y}; + gc.fillPolygon(xx, yy, 4); if (annotationCheck){ drawWake(gc, x, y, speed, color, heading); } } + /** + * Inner class for creating point so that you can rotate it around origin point. + */ + class Point { + + double x, y; + + Point (double x, double y) { + this.x = x; + this.y = y; + } + + void rotate(double angle) { + double oldX = x; + double oldY = y; + this.x = oldX * Math.cos(angle) - oldY * Math.sin(angle); + this.y = oldX * Math.sin(angle) + oldY * Math.cos(angle); + + } + } + /** * Draws the course. */