Jump to content
Swizzbeat

Polygon Area

Recommended Posts

import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.Entity;

import java.awt.*;

/**
 * Created with IntelliJ IDEA
 * User: Anthony
 * Date: 3/23/2014
 */

public class PolygonArea {

	private Polygon polygon;

	public PolygonArea(Position... positions) {
		polygon = new Polygon(retrieveXPoints(positions), retrieveYPoints(positions), positions.length);
	}

	public boolean contains(Entity entity) {
		return polygon.contains(entity.getX(), entity.getY());
	}

	public boolean contains(Position position) {
		return polygon.contains(position.getX(), position.getY());
	}

	private int[] retrieveXPoints(Position[] positions) {
		int[] xPoints = new int[positions.length];
		for (int i = 0; i < positions.length; i++)
			xPoints[i] = positions[i].getX();
		return xPoints;
	}

	private int[] retrieveYPoints(Position[] positions) {
		int[] yPoints = new int[positions.length];
		for (int i = 0; i < positions.length; i++)
			yPoints[i] = positions[i].getY();
		return yPoints;
	}

}

Probably could be improved as well as have had more methods implemented/overridden but here's a basic class for creating a polygon area instead of being stuck with just a simple square.

Edited by Swizzbeat
  • Like 1
Link to comment
Share on other sites

import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.Entity;
 
import java.awt.*;
 
/**
 * Created with IntelliJ IDEA
 * User: Anthony
 * Date: 3/23/2014
 */
 
@SuppressWarnings("serial")
public class PolygonArea extends Polygon {
 
public PolygonArea(Position... positions) {
super(retrieveXPoints(positions), retrieveYPoints(positions), positions.length);
}
 
public boolean contains(Entity entity) {
return contains(entity.getX(), entity.getY());
}
 
public boolean contains(Position position) {
return contains(position.getX(), position.getY());
}
 
private static final int[] retrieveXPoints(Position[] positions) {
int[] xPoints = new int[positions.length];
for (int i = 0; i < positions.length; i++)
xPoints[i] = positions[i].getX();
return xPoints;
}
 
private static final int[] retrieveYPoints(Position[] positions) {
int[] yPoints = new int[positions.length];
for (int i = 0; i < positions.length; i++)
yPoints[i] = positions[i].getY();
return yPoints;
}
 
}

 

No idea why you extend polygon then use a local variable polygon.

Also no idea why the hell my indents were removed when I pasted this here...

Edited by Billy
Link to comment
Share on other sites

import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.Entity;
 
import java.awt.*;
 
/**
 * Created with IntelliJ IDEA
 * User: Anthony
 * Date: 3/23/2014
 */
 
@SuppressWarnings("serial")
public class PolygonArea extends Polygon {
 
public PolygonArea(Position... positions) {
super(retrieveXPoints(positions), retrieveYPoints(positions), positions.length);
}
 
public boolean contains(Entity entity) {
return contains(entity.getX(), entity.getY());
}
 
public boolean contains(Position position) {
return contains(position.getX(), position.getY());
}
 
private static final int[] retrieveXPoints(Position[] positions) {
int[] xPoints = new int[positions.length];
for (int i = 0; i < positions.length; i++)
xPoints[i] = positions[i].getX();
return xPoints;
}
 
private static final int[] retrieveYPoints(Position[] positions) {
int[] yPoints = new int[positions.length];
for (int i = 0; i < positions.length; i++)
yPoints[i] = positions[i].getY();
return yPoints;
}
 
}

No idea why you extend polygon then use a local variable polygon.

Also no idea why the hell my indents were removed when I pasted this here...

 

I was overriding some other methods when I made this and never saw that I didn't remove it. Oh well.

 

Also that code wouldn't even compile considering a call to the superclasses constructor has to be done before anything, including method calls.

Link to comment
Share on other sites

This is what I use for my Polygon areas, it has some extra methods that prove to be useful at times.

    package tools;

import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.Entity;

import java.awt.*;


/**
 * Created with IntelliJ IDEA.
 * User: NotoriousPP
 * Date: 2/6/14
 * Time: 8:53 PM
 */
public class Area {

    private Polygon polyArea;

    public Area(Position... cords){
        polyArea = new Polygon();
        addPositions(cords);
    }

    public Area(int xL, int yL, int xH, int yH){
        polyArea = new Polygon();
        polyArea.addPoint(xL, yL);
        polyArea.addPoint(xH, yH);
    }

    public boolean contains(Entity e){
        return polyArea.contains(e.getX(), e.getY());
    }
    public boolean contains(Position p){
        return polyArea.contains(p.getX(), p.getY());
    }
    public Position getCenterTile(){
        int lowX = 0, lowY = 0;
        int highX = 0, highY = 0;
        for(int i : polyArea.xpoints){
            if(lowX == 0 || i < lowX){
                lowX = i;
            }
            if(highX == 0 || i > highX){
                highX = i;
            }
        }
        for(int i : polyArea.ypoints){
            if(lowY == 0 || i < lowY){
                lowY = i;
            }
            if(highY == 0 || i > highY){
                highY = i;
            }
        }
        int x = ((highX + lowX)/2), y = ((highY + lowY)/2);
        return new Position(x,y,0);
    }
    public int getLowestX(){
        int lowX = 0;
        for(int i : polyArea.xpoints){
            if(lowX == 0 || i < lowX){
                lowX = i;
            }
        }
        if(lowX != 0){
            return lowX;
        }
        return -1;
    }
    public int getLowestY(){
        int lowY = 0;
        for(int i : polyArea.ypoints){
            if(lowY == 0 || i < lowY){
                lowY = i;
            }
        }
        if(lowY != 0){
            return lowY;
        }
        return -1;
    }
    public int getHighestX(){
        int highX = 0;
        for(int i : polyArea.xpoints){
            if(highX == 0 || i > highX){
                highX = i;
            }
        }
        if(highX != 0){
            return highX;
        }
        return -1;
    }
    public org.osbot.script.rs2.utility.Area getOSArea(){
        return new org.osbot.script.rs2.utility.Area(getLowestX(), getLowestY(), getHighestX(), getHighestY());
    }
    public int getHighestY(){
        int highY = 0;
        for(int i : polyArea.ypoints){
            if(highY == 0 || i > highY){
                highY = i;
            }
        }
        if(highY != 0){
            return highY;
        }
        return -1;
    }

    private void addPositions(Position... positions){
        for (Position pos : positions){
            polyArea.addPoint(pos.getX(), pos.getY());
        }
    }
}

  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...