Skip to content

Commit

Permalink
Merge pull request #2 from ediazal/points-array
Browse files Browse the repository at this point in the history
SmothCamWorld.points to ArrayList
  • Loading branch information
semperhilaris committed Apr 23, 2016
2 parents 4cd51d0 + ecb4f3e commit 6440f74
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
13 changes: 8 additions & 5 deletions src/com/semperhilaris/smoothcam/SmoothCamDebugRenderer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

package com.semperhilaris.smoothcam;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Matrix4;
Expand All @@ -19,18 +21,19 @@ public SmoothCamDebugRenderer () {
* @param world
* @param projMatrix */
public void render (SmoothCamWorld world, Matrix4 projMatrix) {
SmoothCamPoint[] points = world.getPoints();
ArrayList<SmoothCamPoint> points = world.getPoints();
renderer.setProjectionMatrix(projMatrix);
renderer.begin(ShapeType.Line);
for (int i = 0; i < points.length; i++) {
for (int i = 0, size = points.size(); i < size; i++) {
SmoothCamPoint point = points.get(i);
renderer.setColor(0, 1, 0, 1);
renderer.circle(points[i].getX(), points[i].getY(), points[i].getInnerRadius());
if (points[i].getPolarity() == SmoothCamPoint.REPULSE) {
renderer.circle(point.getX(), point.getY(), point.getInnerRadius());
if (point.getPolarity() == SmoothCamPoint.REPULSE) {
renderer.setColor(1, 0, 0, 1);
} else {
renderer.setColor(0, 0, 1, 1);
}
renderer.circle(points[i].getX(), points[i].getY(), points[i].getOuterRadius());
renderer.circle(point.getX(), point.getY(), point.getOuterRadius());
}
renderer.setColor(0, 0, 0, 1);
SmoothCamSubject subject = world.getSubject();
Expand Down
4 changes: 2 additions & 2 deletions src/com/semperhilaris/smoothcam/SmoothCamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
Expand Down Expand Up @@ -273,7 +273,7 @@ public void render () {
camera.update();

Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

debugRenderer.render(world, camera.combined);

Expand Down
27 changes: 11 additions & 16 deletions src/com/semperhilaris/smoothcam/SmoothCamWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
package com.semperhilaris.smoothcam;

import java.util.ArrayList;
import java.util.Arrays;

/** Provides smooth camera movement between one {@link SmoothCamSubject} and a set of {@link SmoothCamPoint}
* @author David Froehlich <[email protected]> */
public class SmoothCamWorld {
private SmoothCamSubject subject;
private SmoothCamBoundingBox boundingBox = new SmoothCamBoundingBox(0, 0);
private SmoothCamPoint[] points = {};
private ArrayList<SmoothCamPoint> points = new ArrayList<SmoothCamPoint>(15);
private float x = 0f;
private float y = 0f;
private float zoom = 1f;
Expand All @@ -28,21 +27,16 @@ public SmoothCamWorld (SmoothCamSubject subject) {
/** Adds a {@link SmoothCamPoint} to the world
* @param point */
public void addPoint (SmoothCamPoint point) {
SmoothCamPoint[] newPoints = new SmoothCamPoint[points.length + 1];
newPoints[0] = point;
System.arraycopy(points, 0, newPoints, 1, points.length);
points = newPoints;
points.add(point);
}

/** Removes a {@link SmoothCamPoint} from the world
* @param point */
public void removePoint (SmoothCamPoint point) {
ArrayList<SmoothCamPoint> temp = new ArrayList<SmoothCamPoint>(Arrays.asList(points));
temp.remove(point);
points = temp.toArray(new SmoothCamPoint[temp.size()]);
points.remove(point);
}

public SmoothCamPoint[] getPoints () {
public ArrayList<SmoothCamPoint> getPoints () {
return points;
}

Expand Down Expand Up @@ -127,12 +121,13 @@ public SmoothCamPoint getNearestPoint (SmoothCamPoint p1) {
double distance = 0;
double currentBest = 0;
SmoothCamPoint nearestPoint = new SmoothCamPoint();
for (int i = 0; i < points.length; i++) {
distance = getDistance(p1, points[i]);
if (distance <= points[i].getOuterRadius()) {
return points[i];
for (int i = 0, size = points.size(); i < size; i++) {
SmoothCamPoint point = points.get(i);
distance = getDistance(p1, point);
if (distance <= point.getOuterRadius()) {
return point;
} else if (distance < currentBest || currentBest == 0) {
nearestPoint = points[i];
nearestPoint = point;
currentBest = distance;
}
}
Expand All @@ -145,7 +140,7 @@ public SmoothCamPoint getNearestPoint (SmoothCamPoint p1) {
* camera focus switches completely to the point of interest. */
public void update () {
float coeff = 0f;
if (points.length > 0) {
if (points.size() > 0) {
SmoothCamPoint nearestPoint = getNearestPoint(this.subject);
double distance = getDistance(subject, nearestPoint);
if (distance > nearestPoint.getOuterRadius()) {
Expand Down

0 comments on commit 6440f74

Please sign in to comment.