Jump to content
Brainfree

Model Convexhaulgorithm

Recommended Posts

This algorithm will extract the bounding points of a given Entities model. This could be used for a more precise interacting, Triangulation, Notification, and just plain looking cool. :^)

Disclaimers: The utilities methods for model to points is not working perfectly right now, and it'll cause some weird issues, but its stable.


Example:

ss%20%282013-04-14%20at%2006.37.00%29.jpg

The Code:

import org.osbot.script.Script;import org.osbot.script.ScriptManifest;import org.osbot.script.rs2.model.Entity;import org.osbot.script.rs2.model.Model;import org.osbot.script.rs2.model.Player;import org.osbot.script.rs2.utility.Utilities;import java.awt.*;import java.util.Arrays;import java.util.HashSet;import java.util.Iterator;@ScriptManifest(author = "Brainfree", version = 1.0D, name = "OSModelHaulgorithm",        info = "Constructs a Convex Haul around a Entities model.")public class OSModelHaulgorithm extends Script {    public void onPaint(Graphics g) {        for (Player current_player : client.getLocalPlayers()) {            Polygon haul = buildEntityConvexPolygon(current_player);            if(haul == null) return;            ((Graphics2D) g).draw(haul);        }    }    public Polygon buildEntityConvexPolygon(Entity gameObject) {        PointSeries structure = buildModelConvexHaul(                gameObject.getModel(),                gameObject.getGridX(),                gameObject.getGridY()        );        if (structure == null) return null;        return new Polygon(structure.x_points, structure.y_points, structure.num_points);    }    public PointSeries buildModelConvexHaul(Model model, int gridX, int gridY) {        try {            Point[] points = Utilities.getModelScreenPoints(this.getBot(), gridX, gridY, model);            if (points == null || points.length <= 2) return null; //can't do much with a line.            int[][] data = reflectClean(points);            int[] x_cluster = new int[data.length], y_cluster = new int[data.length];            for (int i = 0; i < data.length; i++) {                x_cluster[i] = data[i][0];                y_cluster[i] = data[i][1];            }            PointSeries data_destination = new PointSeries();            reflectHaul(x_cluster, y_cluster, data.length, data_destination);            data_destination.filter(0); //empty element slots.            return data_destination;        } catch (Exception ignored) {} //Utility errors.        return null;    }    public int[][] reflectClean(Point[] points) {        HashSet<Point> nodes = new HashSet<Point>(Arrays.asList(points)); //cheaper then clear.        int[][] data_matrix = new int[nodes.size()][2];        Iterator<Point> data = nodes.iterator();        Point cur;        for (int i = 0; i < nodes.size(); i++) {            cur = data.next();            data_matrix[i][0] = cur.x;            data_matrix[i][1] = cur.y;        }        return data_matrix;    }    public static class PointSeries {        private int[] x_points;        private int[] y_points;        private int num_points = 0;        public PointSeries(int initialCapacity) {            this.x_points = new int[initialCapacity];            this.y_points = new int[initialCapacity];        }        public PointSeries() {            this(10);        }        public void filter(int invalid) {            int valid_size = 0;            for (int i = 0; i < num_points; i++)                if (x_points[i] != invalid && y_points[i] != invalid) valid_size++;            int[] x_dest = new int[valid_size], y_dest = new int[valid_size];            int x, y;            for (int i = 0; i < num_points; i++) {                if ((x = x_points[i]) != invalid && (y = y_points[i]) != invalid) {                    x_dest[i] = x;                    y_dest[i] = y;                }            }            x_points = x_dest;            y_points = y_dest;            num_points = valid_size;        }        private void expand(int newCapacity) {            x_points = Arrays.copyOf(x_points, newCapacity);            y_points = Arrays.copyOf(y_points, newCapacity);        }        public void addPoint(int x, int y) {            if (num_points + 1 > x_points.length)                expand(((num_points * 3) / 2) + 1);            x_points[num_points] = x;            y_points[num_points] = y;            num_points++;        }    }    public boolean small(int[] xPoints, int[] yPoints, int current, int smallest, int i) {        int xa, ya, xb, yb, val;        xa = xPoints[smallest] - xPoints[current];        xb = xPoints[i] - xPoints[current];        ya = yPoints[smallest] - yPoints[current];        yb = yPoints[i] - yPoints[current];        val = xa * yb - xb * ya;        if (val > 0)            return true;        else if (val < 0)            return false;        else {            if (xa * xb + ya * yb < 0)                return false;            else {                if (xa * xa + ya * ya > xb * xb + yb * yb)                    return true;                else                    return false;            }        }    }    public void reflectHaul(            int[] x_cluster, int[] y_cluster, int numPoints, PointSeries destination) {        int min = 0, smallest;        for (int i = 1; i < numPoints; i++) {            if (y_cluster[i] == y_cluster[min]) {                if (x_cluster[i] < x_cluster[min])                    min = i;            } else if (y_cluster[i] < y_cluster[min])                min = i;        }        int current = min;        do {            destination.addPoint(x_cluster[current], y_cluster[current]);            smallest = 0;            if (smallest == current)                smallest = 1;            for (int i = 0; i < numPoints; i++) {                if ((current == i) || (smallest == i))                    continue;                if (small(x_cluster, y_cluster, current, smallest, i))                    smallest = i;            }            current = smallest;        } while (current != min);    }}

 

 

Have fun!

Edited by Brainfree
  • 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...