Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 07-17-2011, 03:17 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
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};


Last edited by WhiteDragon; 07-17-2011 at 05:53 AM.. Reason: Minor docs error.
Reply With Quote
  #2  
Old 07-17-2011, 05:34 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Nice work!
__________________
Quote:
Reply With Quote
  #3  
Old 07-17-2011, 05:53 AM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Looks great, thanks for contributing this.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #4  
Old 07-17-2011, 06:10 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Thanks guys. What we really need now is a GS2 package system (so I don't need to write "depends on quicksort" ). I also need to clean up my documentation generator and release it.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 04:42 PM.


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