Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+18 votes
asked in CSC335 Fall 2022 by (1 point)

1 Answer

+12 votes

You can make a triangle with drawShape() code like this:

	Rectangle2D rect = getBoundingRect();
	
	double x1 = rect.getMinX();
	double y1 = rect.getMinY();
	double x2 = x1 + rect.getWidth();
	double y2 = y1;
	double x3 = x1;
	double y3 = y1 + rect.getHeight();
	double xs[] = new double[] {x1, x2, x3};
	double ys[] = new double[] {y1, y2, y3};
	
	g.strokePolygon(xs, ys, 3);

If you want a triangle with the top corner pointing up, then you can do this instead:

	Rectangle2D rect = getBoundingRect();
	
	double x1 = rect.getMinX()+ rect.getWidth()/2;
	double y1 = rect.getMinY();
	double x2 = rect.getMinX() + rect.getWidth();
	double y2 = rect.getMinY()+ rect.getHeight();
	double x3 = rect.getMinX();
	double y3 = rect.getMinY()+ rect.getHeight();
	double xs[] = new double[] {x1, x2, x3};
	double ys[] = new double[] {y1, y2, y3};
	
	g.strokePolygon(xs, ys, 3);

Then it would be possible to make an equilateral triangle if you create a Triangle with a height that is equal to the width * sqrt(3) / 2 ... but the practice exam is not really meant to test your geometry skills. Any kind of triangle you can draw is fine, as long as it is within the "bounding box" for the figure.

answered by (4.9k points)
...