I'm working on developing my own engine, and at the moment, I have a line segment class that, given a starting x and y, and a ending x and y, it generates all the points based on a 0.02f unit size. What I mean is that it works like pixels. I only want a solid unit square to light up if the line approaches it in a way similar to Bresenham's Algorithm. It stores these points in a array using my point class (which consists of a x(float), y(float), z(float), and velocity(Vector) and returns this array. When I iterate through the array some of the points come back as null and I've tried checking for when one of the points it just created is null, and its able to give me valid x and y for the point (the reason why its only x and y, is because for now, im ignoring the z axis and am only trying to simulate a pixel screen on a 2d surface, so the z axis is ignored), yet the point still says its null. And its several points, and it changes each time (the line is randomly generated). LineSegment class is as follows:
import java.math.BigDecimal; import javax.media.j3d.Appearance; import javax.media.j3d.Material; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.vecmath.Color3f; import javax.vecmath.Vector3f; import com.sun.j3d.utils.geometry.Box; public class LineSegment { float x1, x2; float y1, y2; Integer count = 0; public LineSegment(float x1, float x2, float y1, float y2) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } public static float round(float d, int decimalPlace) { BigDecimal bd = new BigDecimal(Float.toString(d)); bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP); return bd.floatValue(); } public void drawSegmentPoint(Point point) { Color3f color = new Color3f(1.0f, 0.1f, 0.1f); if (count == 0) { color = new Color3f(1.0f, 0.1f, 0.1f); count++; } else if (count == 1) { color = new Color3f(0.1f, 1.0f, 0.1f); count++; } else if (count == 2) { color = new Color3f(0.1f, 0.1f, 1.0f); count = 0; } Appearance app = new Appearance(); Material mat = new Material(); mat.setDiffuseColor(color); mat.setSpecularColor(color); app.setMaterial(mat); Box box = new Box(0.01f, 0.01f, 0.01f, app); TransformGroup tg = new TransformGroup(); Transform3D transform = new Transform3D(); Vector3f vector = new Vector3f(point.x, point.y, point.z); transform.setTranslation(vector); tg.setTransform(transform); tg.addChild(box); Main.segmentGroup.addChild(tg); } public Point[] returnPointsInSegment() { float x1 = this.x1; float y1 = this.y1; float x2 = this.x2; float y2 = this.y2; float z = 0.0f; Point[] pointArray; float dx = x2 - x1; float dy = y2 - y1; if ((dy / dx) > 1 || (dy / dx) < -1) { if (y2 < y1) { float tempx1 = x2; float tempx2 = x1; x1 = tempx1; x2 = tempx2; float tempy1 = y2; float tempy2 = y1; y1 = tempy1; y2 = tempy2; } } else { if (x2 < x1) { float tempx1 = x2; float tempx2 = x1; x1 = tempx1; x2 = tempx2; float tempy1 = y2; float tempy2 = y1; y1 = tempy1; y2 = tempy2; } } if (x2 == x1 || (dy / dx) > 1 || (dy / dx) < -1) { float ry1 = y1; float ry2 = y2; Integer je = (int) Math.floor(round(ry1 * 100.0f, 0)); if ((je % 2) != 0) { ry1 -= 0.01f; } ry1 = round(ry1, 2); Integer je2 = (int) Math.floor(round(ry2 * 100.0f, 0)); if ((je2 % 2) != 0) { ry2 += 0.01f; } ry2 = round(ry2, 2); pointArray = new Point[(int) (Math .floor(Math.abs(ry2 - ry1) / 0.02f)) + 1]; } else { float rx1 = x1; float rx2 = x2; Integer je = (int) Math.floor(round(rx1 * 100.0f, 0)); if ((je % 2) != 0) { rx1 -= 0.01f; } rx1 = round(rx1, 2); Integer je2 = (int) Math.floor(round(rx2 * 100.0f, 0)); if ((je2 % 2) != 0) { rx2 += 0.01f; } rx2 = round(rx2, 2); pointArray = new Point[(int) (Math .floor(Math.abs(rx2 - rx1) / 0.02f)) + 1]; } if (dx == 0) { for (float y = 0; y1 + y <= y2; y += 0.02) { y = round(y, 2); pointArray[(int) Math.floor(y / 0.02f)] = new Point(x1, y1 + y, z, new Vector(0.0f, 0.0f, 0.0f)); } return pointArray; } float b = y1 - ((dy / dx) * x1); if (dy / dx > 1 || dy / dx < -1) { Integer je = (int) Math.floor(round(y1 * 100.0f, 0)); if ((je % 2) != 0) { y1 -= 0.01f; } y1 = round(y1, 2); for (float y = 0; y1 + y <= y2; y += 0.02) { round(y, 2); float x = (((y + y1) - b) / (dy / dx)); x = round(x, 2); Integer te = (int) Math.floor(round(x * 100.0f, 0)); if ((te % 2) != 0) { x -= 0.01f; } Integer index = (int) Math.floor(y / 0.02f); pointArray[index] = new Point(round(x, 2), round(y + y1, 2), z, new Vector(0.0f, 0.0f, 0.0f)); for (Point point : pointArray) { if (point == null) { System.out.println(index + " : Index : " + round(x1 + x, 2) + " : X : " + round(y, 2) + " : Y"); } } } } else { Integer je = (int) Math.floor(round(x1 * 100.0f, 0)); if ((je % 2) != 0) { x1 -= 0.01f; } x1 = round(x1, 2); for (float x = 0; x1 + x <= x2; x += 0.02) { round(x, 2); float y = (((dy / dx) * (x1 + x)) + b); y = round(y, 2); Integer te = (int) Math.floor(round(y * 100.0f, 0)); if ((te % 2) != 0) { y -= 0.01f; } Integer index = (int) Math.floor(x / 0.02f); pointArray[index] = new Point(round(x1 + x, 2), round(y, 2), z, new Vector(0.0f, 0.0f, 0.0f)); for (Point point : pointArray) { if (point == null) { System.out.println(index + " : Index : " + round(x1 + x, 2) + " : X : " + round(y, 2) + " : Y"); } } } } return pointArray; } }
Oh and if I code horribly, I apologize and would deeply appreciate tips and pointers. Thank you.