Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Geometry function pack (https://forums.graalonline.com/forums/showthread.php?t=134263939)

WhiteDragon 07-17-2011 03:17 AM

Geometry function pack
 
Geometry function pack

Some functions for common geometry tasks when doing game-related things.

Note, this depends on quicksort.

Documentation: http://classicgraal.net/geometry.html

Source:
PHP Code:

this.join("algorithm_quicksort_func");
//#CLIENTSIDE

/**
 * Give the three points of the triangle in {x,y} form, then the point.
 *
 * Uses barycentric coordinates.
 *
 * @param array(float) Point one of triangle.
 * @param array(float) Point two of triangle.
 * @param array(float) Point three of triangle.
 * @param array(float) Point.
 * @return int 0 if it is not in the triangle, 1 if it is.
 */
function pointInTriangle(temp.atemp.btemp.ctemp.p) {
  
temp.v0 this.vsubtract(temp.ctemp.a);
  
temp.v1 this.vsubtract(temp.btemp.a);
  
temp.v2 this.vsubtract(temp.ptemp.a);
  
  
temp.d00 this.vdot(temp.v0temp.v0);
  
temp.d01 this.vdot(temp.v0temp.v1);
  
temp.d02 this.vdot(temp.v0temp.v2);
  
temp.d11 this.vdot(temp.v1temp.v1);
  
temp.d12 this.vdot(temp.v1temp.v2);
  
  
temp.invDenom / (temp.d00 temp.d11 temp.d01 temp.d01);
  
temp.= (temp.d11 temp.d02 temp.d01 temp.d12) * temp.invDenom;
  
temp.= (temp.d00 temp.d12 temp.d01 temp.d02) * temp.invDenom;
  
  return (
temp.0) && (temp.0) && (temp.temp.1);
}

/**
 * Internal function... 2d dot product.
 */
function vdot(temp.v0temp.v1) {
  return 
temp.v0[0]*temp.v1[0] + temp.v0[1]*temp.v1[1];
}

/**
 * Internal function... 2d vector subtraction.
 */
function vsubtract(temp.v0temp.v1) {
  return {
temp.v1[0] - temp.v0[0], temp.v1[1] - temp.v0[1]};
}


/**
 * Point in polygon.
 *
 * @param array(array(float)) An array of {x,y} pairs.
 * @param array(float) An {x,y} pair.
 * @return int If the point is in the polygon.
 */
function pointInPolygon(temp.polytemp.pt) {
  
temp.npol temp.poly.size();
  
temp.i;
  
temp.j;
  
temp.0;
  for (
temp.0temp.temp.npol-1temp.temp.npoltemp.temp.i++) {
    if ((((
temp.poly[temp.i][1] <= temp.pt[1]) && (temp.pt[1] < temp.poly[temp.j][1])) ||
    ((
temp.poly[temp.j][1] <= temp.pt[1]) && (temp.pt[1] < temp.poly[temp.i][1]))) &&
    (
temp.pt[0] < (temp.poly[temp.j][0] - temp.poly[temp.i][0]) * (temp.pt[1] - temp.poly[temp.i][1]) / (temp.poly[temp.j][1] - temp.poly[temp.i][1]) + temp.poly[temp.i][0])) {
      
temp.= !temp.c;
    }
  }
  return 
temp.c;
}



/**
 * Convex hull... given an array of points, form a convex polygon which contains all of them.
 * Uses the gift wrap algorithm.
 *
 * @param array(array(float)) An array of {x,y} pairs.
 * @return array(array(float)) An array of {x,y} pairs forming the convex polygon around the input pairs.
 */
function convexHull(temp.points) {
  
temp.ordered.copyfrom(temp.points);
  
this.lexographicOrder(temp.ordered);
  
temp.hull = {temp.ordered[0]};
  for (
temp.temp.hull) {
    
temp.this.convexHullNext(temp.pointstemp.p);
    if ((@
temp.q) != (@temp.hull[0])) {
      
temp.hull.add(temp.q);
    }
  }
  return 
temp.hull;
}


// internal
function convexHullDist(temp.ptemp.q) {
  
temp.dx temp.q[0] - temp.p[0];
  
temp.dy temp.q[1] - temp.p[1];
  
  return 
temp.dx temp.dx temp.dy temp.dy;
}

// internal
function convexHullNext(temp.pointstemp.p) {
  
temp.temp.p;
  for (
temp.temp.points) {
    
temp.this.convexHullTurn(temp.ptemp.qtemp.r);
    if (
temp.== -|| temp.== && this.convexHullDist(temp.ptemp.r) > this.convexHullDist(temp.ptemp.q)) {
      
temp.temp.r;
    }
  }
  return 
temp.q;
}

// internal
function convexHullTurn(temp.ptemp.qtemp.r) {
  
temp.= (temp.q[0] - temp.p[0])*(temp.r[1] - temp.p[1]) - (temp.r[0] - temp.p[0])*(temp.q[1] - temp.p[1]);
  if (
temp.0) {
    return -
1;
  } else if (
temp.== 0) {
    return 
0;
  } else {
    return 
1;
  }
}

// internal
function lexographicOrder(temp.ps) {
  
temp.cmp = function (temp.atemp.b) {
    if (
temp.a[0] < temp.b[0]) {
      return -
1;
    } else if (
temp.a[0] == temp.b[0] && temp.a[1] <= temp.b[1]) {
      return -
1;
    } else {
      return 
1;
    }
  };
  
this.quicksort(temp.ps.link(), temp.cmp);
}



/**
 * Bounding rectangle.
 * Get a rectangle that inscribes the given polygon.
 *
 * @param array(array(float)) An array of {x,y} points that form a polygon.
 * @return array(float) A {x,y,w,h} array describing the rectangle inscribing the polygon.
 */
function boundingRectangle(temp.ps) {
  
temp.lowestX temp.ps[0][0];
  
temp.lowestY temp.ps[0][1];
  
temp.highestX temp.ps[0][0];
  
temp.highestY temp.ps[0][1];
  
temp.pss temp.ps.size();
  for (
temp.0temp.temp.psstemp.i++) {
    
temp.temp.ps[temp.i];
    
temp.lowestX min(temp.lowestXtemp.p[0]);
    
temp.lowestY min(temp.lowestYtemp.p[1]);
    
temp.highestX max(temp.highestXtemp.p[0]);
    
temp.highestY max(temp.highestYtemp.p[1]);
  }
  
  
temp.temp.lowestX;
  
temp.temp.lowestY;
  
temp.width temp.highestX temp.lowestX;
  
temp.height temp.highestY temp.lowestY;
  
  return {
temp.xtemp.ytemp.widthtemp.height};



fowlplay4 07-17-2011 05:34 AM

Nice work!

Tigairius 07-17-2011 05:53 AM

Looks great, thanks for contributing this. :)

WhiteDragon 07-17-2011 06:10 AM

Thanks guys. What we really need now is a GS2 package system (so I don't need to write "depends on quicksort" :p). I also need to clean up my documentation generator and release it.


All times are GMT +2. The time now is 09:12 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.