Question: You are helping your friend make a simple graphics library for drawing shapes. They only know how to draw triangles and don't like geometry so

You are helping your friend make a simple graphics library for drawing shapes. They only know how to draw triangles and don't like geometry so you are helping by writing the code that represents coordinates, the Coord class, shapes, the Square class, and operations on those shapes like translate (Links to an external site.)Links to an external site., rotateAboutOrigin (Links to an external site.)Links to an external site., and scale. Your friend has written some code and has asked you to fill in the methods and things with TODO comments. They don't care if you add extra members and methods but you can't change any method definitions.

Additionally, your friend is holding a programming-art competition and promises to give something to whoever can make art and get the most votes using their/your shape drawing code. You decide to implement your programming-art code in ArtSubmission.draw.

Luckily, your friend has included an implementation/subclass of Drawer called SVGDrawer which you can use to write .svg files so you can save your art and look at it (e.g. in a browser).

Starter Code:

package hw02;

public class Color { private double red, green, blue;

public Color(double red, double green, double blue) { if (red < 0 || red > 1 || green < 0 || green > 1 || blue < 0 || blue > 1) { throw new IllegalArgumentException("Color red/green/blue values must all be between 0 and 1."); } this.red = red; this.green = green; this.blue = blue; }

public String htmlColorCode() { int r = (int)(red * 255); int g = (int)(green * 255); int b = (int)(blue * 255); return String.format("#%02x%02x%02x", r, g, b); }

@Override public String toString() { return htmlColorCode(); } }

package hw02;

public class Coord { // TODO: Add the members like "double x, y" to represent this coordinate.

public static Coord fromXAndY(double x, double y) { // TODO }

public static Coord fromAngleAndDistance(double angle, double distance) { // TODO }

public Coord(Coord other) { // this(...); }

public double getX() { return x; } public double getY() { return y; }

public String toString() { return String.format("(%f, %f)", getX(), getY()); } }

package hw02;

// https://en.wikipedia.org/wiki/Regular_polygon public class RegularPolygon extends Drawable { // The implementation below is ONLY a suggestion. You may use a different internal representation // for your Regular Polygon if you want to. private Coord[] coords; private Coord center;

/** * Construct a regular polygon where the first Coord is on the x-axis (at Coord.fromXAndY(1, 0) * and all Coords are 1.0 distance from the origin. * @param numSides */ public RegularPolygon(int numSides) { coords = new Coord[numSides]; for (int i = 0; i < numSides; i++) { coords[i] = Coord.fromAngleAndDistance(i * 2 * Math.PI / numSides, 1.0); } center = Coord.origin(); }

public Coord[] getCoords() { return coords; }

public void draw(Drawer drawer, Color color) { // TODO }

public void translate(Coord amount) { // TODO }

public void rotateAboutOrigin(double radians) { // TODO }

public void scale(double size) { // TODO } }

package hw02;

public class Square extends RegularPolygon { /** * Construct a square with sideLength * and it's sides parallel/perpendicular to the x and y axis. */ public Square(double sideLength) { super(4); rotateAboutOrigin(Math.PI / 4.0); scale(sideLength / Math.sqrt(2.0)); }

// TODO/OPTIONAL: Override the draw(...) method to draw using only two triangles instead of four. }

package hw02;

public class Triangle extends Drawable { // TODO: Add members to represent the triangle

public Triangle(Triangle other) { // TODO }

public Triangle(Coord a, Coord b, Coord c) { // TODO }

public Triangle(Coord[] coords) { // TODO // Should throw IllegalArgumentException if coords.length != 3. }

public Coord[] getCoords() { // TODO }

public void draw(Drawer drawer, Color color) { drawer.drawTriangle(this, color); }

public void translate(Coord amount) { // TODO }

public void rotateAboutOrigin(double radians) { // TODO }

public void scale(double size) { // TODO } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!