📄 geometry.pde
字号:
/**
* This file is a total mess!!!!...
*
*/
/////////////////////////////////////////////////////////////
// Code adapted from http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/source.c
float magnitude(float x1, float y1, float x2, float y2)
{
return (float)sqrt( x1*x2 + y1*y2 );
}
float [] intersection(float x1, float y1 , float segx1, float segy1, float segx2, float segy2)
{
float LineMag;
float U;
//XYZ Intersection;
float inter[] = new float[2];
LineMag = magnitude( segx1, segy1, segx2, segy2 );
U = ( ( ( x1 - segx1 ) * ( segx2 - segx1 ) ) +
( ( y1 - segy1 ) * ( segy2 - segy1 ) )
) / ( LineMag * LineMag );
if( U < 0.0f || U > 1.0f )
return null; // closest point does not fall within the line segment
inter[0] = segx1 + U * ( segx2 -segx1 );
inter[1] = segy1 + U * ( segy2 - segy1 );
return inter;
}
///////////////////////////////////////////////////////////////////
// Code borrowed and adapted from http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
/**
* * Function to calculate the area of a polygon, according to the algorithm
* * defined at http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
*/
public double calculateArea(float s[][]) {
int i, j, n = s.length;
double area = 0;
for (i = 0; i < n; i++) {
j = (i + 1) % n;
area += s[i][0] * s[j][1];
area -= s[j][0] * s[i][1];
}
area /= 2.0;
return (area);
}
/**
* * Function to calculate the center of mass for a given polygon, according
* * ot the algorithm defined at
* * http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
* *
* * @param polyPoints
* * array of points in the polygon
* * @return point that is the center of mass
*/
public Point2D.Double calculateCenterOfMass(float s[][]) {
double cx = 0, cy = 0;
double area = calculateArea(s);
// could change this to Point2D.Float if you want to use less memory
int i, j, n = s.length;
double factor = 0;
for (i = 0; i < n; i++) {
j = (i + 1) % n;
factor = (s[i][0] * s[j][1]
- s[j][0] * s[i][1]);
cx += (s[i][0] + s[j][0]) * factor;
cy += (s[i][1] + s[j][1]) * factor;
}
area *= 6.0f;
factor = 1 / area;
cx *= factor;
cy *= factor;
return new Point2D.Double(cx,cy);
}
////////////////////////////////////////////////////////////
// Code borrowed from http://www.ahristov.com/tutorial/geometry-games/reflections-2d.html
import java.awt.geom.*;
public Point2D.Double reflection(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 ) {
//println(x1 + " "+ y1 + " " + x2 + " " + y2);
Point2D.Double n = normal(x3, y3, x4, y4);
Point i = intersection(x1, y1, x2, y2, x3, y3, x4, y4);
if (i == null) {
return null;
}
//println("normal: " + n);
Point2D.Double r = reflection(x1, y1, (int)(i.getX()), (int)(i.getY()), n.getX(), n.getY());
fill(0, 255, 0);
stroke(0, 255, 0);
ellipse((int)(i.getX()+r.getX()), (int)(i.getY()+r.getY()), 5, 5);
r.setLocation(r.getX()+i.getX(), r.getY()+i.getY());
return r;
}
/**
* Computes the reflected segment at a point of a curve
* @param x1 Starting point of the segment
* @param y1 Starting point of the segment
* @param xi Intersection of the segment with the curve acting as a mirror
* @param yi Intersection of the segment with the curve acting as a mirror
* @param nx Normal of the curve at the point of intersection
* @param ny Normal of the curve at the point of intersection
* @return Reflected segment (has the same norm as the falling segment)
*/
public Point2D.Double reflection(int x1, int y1, int xi, int yi, double nx, double ny ) {
double rx, ry;
double dot = (xi-x1)*nx+(yi-y1)*ny;
rx = (xi-x1)-2*nx*dot;
ry = (yi-y1)-2*ny*dot;
return new Point2D.Double(rx,ry);
}
/**
* * Computes the intersection between two segments.
* * @param x1 Starting point of Segment 1
* * @param y1 Starting point of Segment 1
* * @param x2 Ending point of Segment 1
* * @param y2 Ending point of Segment 1
* * @param x3 Starting point of Segment 2
* * @param y3 Starting point of Segment 2
* * @param x4 Ending point of Segment 2
* * @param y4 Ending point of Segment 2
* * @return Point where the segments intersect, or null if they don't
*/
public Point intersection(
int x1,int y1,int x2,int y2,
int x3, int y3, int x4,int y4
) {
int d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4);
if (d == 0) return null;
int xi = ((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/d;
int yi = ((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/d;
Point p = new Point(xi,yi);
if (xi < Math.min(x1,x2) || xi > Math.max(x1,x2)) return null;
if (xi < Math.min(x3,x4) || xi > Math.max(x3,x4)) return null;
//fill(255, 0, 0);
// ellipse(xi, yi, 5, 5);
return p;
}
/**
* Computes the unitary normal vector of a segment
* @param x1 Starting point of the segment
* @param y1 Starting point of the segment
* @param x2 Ending point of the segment
* @param y2 Ending point of the segment
* @return
*/
public Point2D.Double normal(int x1,int y1,int x2, int y2) {
double nx,ny;
if (x1 == x2) {
nx = Math.signum(y2-y1);
ny = 0;
}
else {
double f = (y2-y1)/(double)(x2-x1);
nx = f*Math.signum(x2-x1)/Math.sqrt(1+f*f);
ny = -1*Math.signum(x2-x1)/Math.sqrt(1+f*f);
}
return new Point2D.Double(nx,ny);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -