Skip to content

Instantly share code, notes, and snippets.

@nullcoding
Created September 19, 2012 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullcoding/3747196 to your computer and use it in GitHub Desktop.
Save nullcoding/3747196 to your computer and use it in GitHub Desktop.
Heptagon Maker
import java.awt.*;
public class Heptagon {
private int x, y, radius;
private double area;
private Color theColor;
private boolean isVisible;
private Polygon polygon;
/**
* Construct a new Heptagon centered at initX, initY with a
* color indicated by initColor. The new Heptagon is visible by
* default.
*
* @param initX the x coordinate of the center of the 7-sided thingy.
* @param initY the y coordinate of the center of the heptagon.
* @param initRadius an arbitrary value of the distance from (initX, initY) to a point at which
* two sides meet at roughly 5(pi)/7 radians
* @param initColor the color of the heptagon.
*/
public Heptagon(int initX, int initY, int initRadius, Color initColor) {
x = initX;
y = initY;
radius = initRadius;
theColor = initColor;
isVisible = true;
polygon = new Polygon();
}
/**
* Increases the size of the heptagon by the specified factor.
* @param factor the amount by which you wish to increase the area of the heptagon.
*/
@Override
public void scale(double factor) {
if (factor > 0) {
radius = (int) (Math.round(radius * factor));
}
}
/**
* Move the center of the heptagon.
* @param newX
* @param newY
*/
public void move(int newX, int newY) {
x = newX;
y = newY;
polygon.reset();
}
/**
* Draws the heptagon using a Graphics object.
* Points are calculated and placed based on the radius relative
* to the starting x and y co-ordinates using basic trigonometric functions
* and therefore may not create a perfect regular heptagon where all sides
* meet at 5(pi)/7 radians.
* @param g a pointer to a pre-initialized Graphics drawing object.
*/
public void draw(Graphics g) {
g.setColor(theColor);
for (int i = 0; i < 7; i++){
polygon.addPoint((int) (x + radius * Math.cos(i * 2 * Math.PI / 7)),
(int) (y + radius * Math.sin(i * 2 * Math.PI / 7)));}
g.drawPolygon(polygon);
g.fillPolygon(polygon);
}
/**
* Calculates and returns the area of the heptagon accurate to 2 decimal places
* using the formula A = 7/4 * a^2 * cot(pi/7) where a is the length of a side
* calculated based on the radius relative to the starting x and y co-ordinates.
* @return area the approximate area of the heptagon, accurate to ~2 decimal places.
*/
public Double getArea() {
// imagine we make points 1 and 2 from the draw() method above
double x1 = x + radius * Math.cos(1 * 2 * Math.PI / 7);
double x2 = x + radius * Math.cos(2 * 2 * Math.PI / 7);
double y1 = y + radius * Math.sin(1 * 2 * Math.PI / 7);
double y2 = y + radius * Math.sin(2 * 2 * Math.PI / 7);
// calculate the distance between these points
double xd = Math.abs(x1-x2);
double yd = Math.abs(y1-y2);
// and finally, determine the length of one side of the heptagon
double side = Math.sqrt((Math.pow(xd, 2))+(Math.pow(yd, 2)));
// now implement the area formula mentioned above
area = ((7/4) * Math.pow(side, 2)) * (1/(Math.tan((Math.PI / 7))));
return area;
}
/**
* Tells you the current color being used for drawing.
* @return theColor the current color being used for drawing.
*/
@Override
public Color getColor() {
return theColor;
}
/**
* Sets a new color for drawing.
* @param newColor the new color you wish to use.
*/
@Override
public void setColor(Color newColor) {
theColor = newColor;
}
/**
* Sets the heptagon to visible or not.
* @param visible true for visible, false for hidden.
*/
@Override
public void setVisible(boolean visible) {
isVisible = visible;
}
/**
* Tells you whether the heptagon is visible.
* @return isVisible true if visible, false if hidden.
*/
@Override
public boolean isVisible() {
return isVisible;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment