Removed center() method, added getCenter(), updated examples

This commit is contained in:
codeanticode
2012-04-11 22:27:21 +00:00
parent 611ac827e6
commit e4dbc6fd9c
5 changed files with 67 additions and 216 deletions
+5
View File
@@ -393,6 +393,11 @@ public class PShape implements PConstants {
}
public PVector getCenter() {
return new PVector();
}
/**
* Return true if this shape is 3D. Defaults to false.
*/
@@ -36,7 +36,8 @@ class Particle {
velocity = new PVector(cos(a), sin(a));
velocity.mult(speed);
lifespan = 255;
part.center(x,y);
PVector center = part.getCenter();
part.translate(x - center.x, y - center.y);
}
boolean isDead() {
@@ -62,7 +62,8 @@ void draw () {
if (lifetimes[n] == 0) {
// Re-spawn dead particle
part.center(mouseX, mouseY);
PVector center = part.getCenter();
part.translate(mouseX - center.x, mouseY - center.y);
float angle = random(0, TWO_PI);
float s = random(0.5 * speed, 0.5 * speed);
velocities[n].x = s * cos(angle);
@@ -1938,7 +1938,7 @@ public class PGraphicsOpenGL extends PGraphics {
} else if (type == TRIANGLE_STRIP) {
shape = new PShape3D(parent, PShape.GEOMETRY);
shape.setKind(TRIANGLE_STRIP);
} else if (type == QUADS || type == QUADS) {
} else if (type == QUAD || type == QUADS) {
shape = new PShape3D(parent, PShape.GEOMETRY);
shape.setKind(QUADS);
} else if (type == QUAD_STRIP) {
@@ -7862,135 +7862,7 @@ public class PGraphicsOpenGL extends PGraphics {
return newSize;
}
public void center(float cx, float cy) {
int index;
// Computing current center
float cx0 = 0;
float cy0 = 0;
for (int i = 0; i < fillVertexCount; i++) {
index = 3 * i;
cx0 += fillVertices[index++];
cy0 += fillVertices[index ];
}
for (int i = 0; i < lineVertexCount; i++) {
index = 3 * i;
cx0 += lineVertices[index++];
cy0 += lineVertices[index ];
}
for (int i = 0; i < pointVertexCount; i++) {
index = 3 * i;
cx0 += pointVertices[index++];
cy0 += pointVertices[index ];
}
int nt = fillVertexCount + lineVertexCount + pointVertexCount;
if (0 < nt) {
cx0 /= nt;
cy0 /= nt;
}
float tx = cx - cx0;
float ty = cy - cy0;
if (0 < fillVertexCount) {
for (int i = 0; i < fillVertexCount; i++) {
index = 3 * i;
fillVertices[index++] += tx;
fillVertices[index ] += ty;
}
}
if (0 < lineVertexCount) {
for (int i = 0; i < lineVertexCount; i++) {
index = 3 * i;
lineVertices[index++] += tx;
lineVertices[index ] += ty;
index = 4 * i;
lineDirWidths[index++] += tx;
lineDirWidths[index ] += ty;
}
}
if (0 < pointVertexCount) {
for (int i = 0; i < pointVertexCount; i++) {
index = 3 * i;
pointVertices[index++] += tx;
pointVertices[index ] += ty;
}
}
}
public void center(float cx, float cy, float cz) {
int index;
// Computing current center
float cx0 = 0;
float cy0 = 0;
float cz0 = 0;
for (int i = 0; i < fillVertexCount; i++) {
index = 3 * i;
cx0 += fillVertices[index++];
cy0 += fillVertices[index++];
cz0 += fillVertices[index ];
}
for (int i = 0; i < lineVertexCount; i++) {
index = 3 * i;
cx0 += lineVertices[index++];
cy0 += lineVertices[index++];
cz0 += lineVertices[index ];
}
for (int i = 0; i < pointVertexCount; i++) {
index = 3 * i;
cx0 += pointVertices[index++];
cy0 += pointVertices[index++];
cz0 += pointVertices[index ];
}
int nt = fillVertexCount + lineVertexCount + pointVertexCount;
if (0 < nt) {
cx0 /= nt;
cy0 /= nt;
cz0 /= nt;
}
float tx = cx - cx0;
float ty = cy - cy0;
float tz = cz - cz0;
if (0 < fillVertexCount) {
for (int i = 0; i < fillVertexCount; i++) {
index = 3 * i;
fillVertices[index++] += tx;
fillVertices[index++] += ty;
fillVertices[index ] += tz;
}
}
if (0 < lineVertexCount) {
for (int i = 0; i < lineVertexCount; i++) {
index = 3 * i;
lineVertices[index++] += tx;
lineVertices[index++] += ty;
lineVertices[index ] += tz;
index = 4 * i;
lineDirWidths[index++] += tx;
lineDirWidths[index++] += ty;
lineDirWidths[index ] += tz;
}
}
if (0 < pointVertexCount) {
for (int i = 0; i < pointVertexCount; i++) {
index = 3 * i;
pointVertices[index++] += tx;
pointVertices[index++] += ty;
pointVertices[index ] += tz;
}
}
}
public int getCenter(PVector v) {
public int getVertexSum(PVector v) {
int index;
for (int i = 0; i < fillVertexCount; i++) {
index = 3 * i;
@@ -467,7 +467,62 @@ public class PShape3D extends PShape {
pg.finalizeVertexBufferObject(glPointIndexBufferID);
}
}
///////////////////////////////////////////////////////////
//
// Query methods
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
public float getDepth() {
return depth;
}
public PVector getCenter() {
PVector center = new PVector();
int count = 0;
if (shapeEnded) {
updateTesselation();
count = getVertexSum(center, count);
if (0 < count) {
center.x /= count;
center.y /= count;
center.z /= count;
}
}
return center;
}
protected int getVertexSum(PVector sum, int count) {
if (family == GROUP) {
for (int i = 0; i < childCount; i++) {
PShape3D child = (PShape3D) children[i];
count += child.getVertexSum(sum, count);
}
} else {
count += tess.getVertexSum(sum);
}
return count;
}
///////////////////////////////////////////////////////////
//
@@ -1355,89 +1410,6 @@ public class PShape3D extends PShape {
// Geometric transformations
//------------------------------------------------------------------------------
// TODO: Remove center, add function to get center from point...
public void center(float cx, float cy) {
if (shapeEnded) {
updateTesselation();
if (family == GROUP) {
PVector center = new PVector();
int count = updateCenter(center, 0);
center.x /= count;
center.y /= count;
float tx = cx - center.x;
float ty = cy - center.y;
setChildHasMatrix(true);
applyMatrix = true;
super.translate(tx, ty);
} else {
PVector vec = new PVector();
int count = tess.getCenter(vec);
vec.x /= count;
vec.y /= count;
float tx = cx - vec.x;
float ty = cy - vec.y;
translate(tx, ty);
}
}
}
public void center(float cx, float cy, float cz) {
if (shapeEnded) {
updateTesselation();
if (family == GROUP) {
PVector center0 = new PVector();
int count = updateCenter(center0, 0);
center0.x /= count;
center0.y /= count;
center0.z /= count;
float tx = cx - center0.x;
float ty = cy - center0.y;
float tz = cz - center0.z;
setChildHasMatrix(true);
applyMatrix = true;
super.translate(tx, ty, tz);
} else {
PVector vec = new PVector();
int count = tess.getCenter(vec);
vec.x /= count;
vec.y /= count;
vec.z /= count;
float tx = cx - vec.x;
float ty = cy - vec.y;
float tz = cz - vec.z;
translate(tx, ty, tz);
}
}
}
protected int updateCenter(PVector vec, int count) {
if (family == GROUP) {
count = updateCenter(vec, count);
} else {
count += tess.getCenter(vec);
}
return count;
}
//------------------------------------------------------------------------------
public void translate(float tx, float ty) {
transform(TRANSLATE, tx, ty);
}