Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Polygons

Options
  • 27-02-2009 3:57pm
    #1
    Registered Users Posts: 23,568 ✭✭✭✭


    Basically have a problem here trying to fill a polygon in java.
    Restrictions being I have to use the drawVertex Polygon() and fillVertexPolygon() methods.

    Also have to convert my polygon from world to local points.
    So far:
    public void initializePolygon() {
    // polygon in world points
    points[xpoints] = new Vector2D(Game.WIDTH /3 -10 , Game.HEIGHT /3 +5);
    points[ypoints] = new Vector2D(Game.WIDTH /3 -10, Game.HEIGHT /3 -5);
    points[zpoints] = new Vector2D(Game.WIDTH /3 -13, Game.HEIGHT /3 +0);

    // polygon in local points
    pointsLocal[xpoints] = new Vector2D(-10, 5);
    pointsLocal[ypoints] = new Vector2D(-10, -5);
    pointsLocal[zpoints] = new Vector2D(-13, 0);
    }

    This is just converting the points from world to screen.

    Then:
    public void drawVertexPolygon(Graphics2D p, int[] points){
    for(int i=0; i<points.length; i++);



    }

    Ok so I know I have to populate an array of x values and an array of y values representing x and y co-ordinates.
    However not entirely sure on:

    (1) I'm assuming I should read in the array of x values in the correct order for them to be in tandem with their corresponding y values?

    (2) I know there's an npoints method to be used which specifies how many points I have in the array however not exactly sure what this does / how it should be implemented?


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Frisbee wrote: »
    (1) I'm assuming I should read in the array of x values in the correct order for them to be in tandem with their corresponding y values??
    Why don't you create a structure/class with three ints x, y and z and use this to represent a Point, and have an array of them? Having separate arrays sounds too error prone IMO.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    There already is a Point2D class, you should probably use that.


  • Registered Users Posts: 23,568 ✭✭✭✭Frisbee


    Been trying this over the weekend and still have gotten basically nowhere with it.

    Should I be trying to read in a seperate array of points or jsut taking them from where they've been initialized?


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Why don't you post up more of the code?


  • Registered Users Posts: 23,568 ✭✭✭✭Frisbee


    It seemed easier to use a seperate polygon class and then call on it where I wanted.
    package comp20050.game.object;

    import java.awt.Rectangle;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.PathIterator;
    import java.awt.geom.Point2D;
    import comp20050.game.Game;
    import comp20050.game.util.Vector2D;
    import comp20050.game.object.Ship;


    public class Polygon{

    public int npoints;
    public int xpoints[];
    public int ypoints[];

    protected Rectangle bounds;

    public Polygon() {
    xpoints = new int[6];
    ypoints = new int[6];
    }

    public Polygon(int xpoints[], int ypoints[], int npoints) {
    if (npoints > xpoints.length || npoints > ypoints.length) { throw new IndexOutOfBoundsException("npoints > xpoints.length || npoints > ypoints.length");
    }
    this.npoints = npoints;
    this.xpoints = new int[npoints];
    this.ypoints = new int[npoints];
    System.arraycopy(xpoints, 0, this.xpoints, 0, npoints);
    System.arraycopy(ypoints, 0, this.ypoints, 0, npoints);
    }

    public void reset() {
    npoints = 0;
    bounds = null;
    }

    public void invalidate() {
    bounds = null;
    }

    public void translate(int deltaX, int deltaY) {
    for (int i = 0; i < npoints; i++) {
    xpoints += deltaX;
    ypoints += deltaY;
    }
    if (bounds != null) {
    bounds.translate(deltaX, deltaY);
    }
    }



    void calculateBounds(int xpoints[], int ypoints[], int npoints) {
    int boundsMinX = Integer.MAX_VALUE;
    int boundsMinY = Integer.MAX_VALUE;
    int boundsMaxX = Integer.MIN_VALUE;
    int boundsMaxY = Integer.MIN_VALUE;

    for (int i = 0; i < npoints; i++) {
    int x = xpoints;
    boundsMinX = Math.min(boundsMinX, x);
    boundsMaxX = Math.max(boundsMaxX, x);
    int y = ypoints;
    boundsMinY = Math.min(boundsMinY, y);
    boundsMaxY = Math.max(boundsMaxY, y);
    }
    bounds = new Rectangle(boundsMinX, boundsMinY,
    boundsMaxX - boundsMinX,
    boundsMaxY - boundsMinY);
    }


    void updateBounds(int x, int y) {
    if (x < bounds.x) {
    bounds.width = bounds.width + (bounds.x - x);
    bounds.x = x;
    }
    else {
    bounds.width = Math.max(bounds.width, x - bounds.x);
    // bounds.x = bounds.x;
    }

    if (y < bounds.y) {
    bounds.height = bounds.height + (bounds.y - y);
    bounds.y = y;
    }
    else {
    bounds.height = Math.max(bounds.height, y - bounds.y);
    // bounds.y = bounds.y;
    }
    }


    public void addPoint(int x, int y) {
    if (npoints == xpoints.length) {
    int tmp[];

    tmp = new int[npoints * 2];
    System.arraycopy(xpoints, 0, tmp, 0, npoints);
    xpoints = tmp;

    tmp = new int[npoints * 2];
    System.arraycopy(ypoints, 0, tmp, 0, npoints);
    ypoints = tmp;
    }
    xpoints[npoints] = x;
    ypoints[npoints] = y;
    npoints++;

    }




    }


    Not sure though how to get it to read in the exact points I want.
    Should I just populate a seperate array?


  • Advertisement
  • Registered Users Posts: 3,945 ✭✭✭Anima


    I wrote some code that I think might help. The way you're doing it now is making you have to do a lot of bookeeping which in turn means code will be a lot slower and more error prone than it needs to be. I think something like below, which uses the Point2D class I suggested earlier, might make things easier to handle in the long run. I haven't compiled it so there might be some errors but the jist of it is there.

    [PHP]public class Polygon
    {
    ArrayList<Point2D> points = null;

    public Polygon() {
    points = new ArrayList<Point2D>();
    }

    public Polygon( Point2D p ) {
    points = new ArrayList<Point2D>();
    points.add(p);
    }

    public void translate( Point2D delta ) {
    for( Point2D p : points ) {
    p.x += delta.x;
    p.y += delta.y;
    }
    }

    public void addPoint( Point2D point ) {
    points.add(point);
    }
    }[/PHP]


Advertisement