public static Polygon3D buildPolygon(LineData ld, Vector points) { int size = ld.size(); int[] cxr = new int[size]; int[] cyr = new int[size]; int[] czr = new int[size]; Point3D[] normals = new Point3D[size]; for (int i = 0; i < size; i++) { int num = ld.getIndex(i); Point3D p = (Point3D) points.elementAt(num); // real coordinates cxr[i] = (int) (p.x); cyr[i] = (int) (p.y); czr[i] = (int) (p.z); normals[i] = p.getNormal(); } Polygon3D p3dr = new Polygon3D(size, cxr, cyr, czr, normals); return p3dr; }
/** * @param a * @param b * @return */ public static Point3D plus(Point3D a, Point3D b) { Point3D c = new Point3D(a.x, a.x, a.x); c.x = a.x + b.x; c.y = a.y + b.y; c.z = a.z + b.z; return c; }
public static void main(String[] args) { Point3D p1 = new Point3D(0.0, 0.0, 0.0); Point3D p2 = new Point3D(4.0, 4.0, 4.0); Line3D l1 = new Line3D(p1, p2); Point3D p3 = new Point3D(0.0, 4.0, 4.0); Point3D p4 = new Point3D(4.0, 0.0, 0.0); Line3D l2 = new Line3D(p3, p4); System.out.println("p1: " + p1); System.out.println("p2: " + p2); System.out.println("l1: " + l1); System.out.println("l1.intersection(p2): " + l1.intersection(p2)); System.out.println("p2.intersection(l1): " + p2.intersection(l1)); System.out.println("l2: " + l2); System.out.println("l2.intersection(l1): " + l2.intersection(l1)); System.out.println("l1.intersection(l2): " + l1.intersection(l2)); System.out.println("Object3D c1 = l1.union(l2)"); Object3D c1 = l1.union(l2); System.out.println("c1: \n" + c1.toString()); System.out.println("p1.union(p4): \n" + p1.union(p4)); System.out.println("c1.intersection(p1.union(p4))"); System.out.println(c1.intersection(p1.union(p4))); System.out.println("c1.union(p3):\n" + c1.union(p3)); }
/** * @param a * @param b * @return */ public static Point3D div(Point3D a, double b) { Point3D c = new Point3D(a.x, a.x, a.x); c.x = a.x / b; c.y = a.y / b; c.z = a.z / b; return c; }
private void dehomog(Point3D pA, Point3D pB, Point3D pC, int color, boolean fill) { Vec3D vA = pA.dehomog(); Vec3D vB = pB.dehomog(); Vec3D vC = pC.dehomog(); cropScene(vA, vB, vC, color, fill); }
public Point3D update(float time) { elapsedTime += time; if (elapsedTime == 0) { returnPos = pos1; } else if (elapsedTime >= 10) // traveling to a planet will take 10 seconds { returnPos.set(pos4.x, pos4.y, pos4.z); } else { float t = elapsedTime / 10f; returnPos.x = (1.0f - t) * (1.0f - t) * (1.0f - t) * pos1.x + 3 * (1.0f - t) * (1.0f - t) * t * pos2.x + 3 * (1.0f - t) * t * t * pos3.x + t * t * t * pos4.x; returnPos.y = (1.0f - t) * (1.0f - t) * (1.0f - t) * pos1.y + 3 * (1.0f - t) * (1.0f - t) * t * pos2.y + 3 * (1.0f - t) * t * t * pos3.y + t * t * t * pos4.y; returnPos.z = (1.0f - t) * (1.0f - t) * (1.0f - t) * pos1.z + 3 * (1.0f - t) * (1.0f - t) * t * pos2.z + 3 * (1.0f - t) * t * t * pos3.z + t * t * t * pos4.z; } return returnPos; }
/** * @param a * @param b * @return */ public static Point3D moins(Point3D a, Point3D b) { Point3D c = new Point3D((a.x - b.x), (a.y - b.y), (a.z - b.z)); c.x = a.x - b.x; c.y = a.y - b.y; c.z = a.z - b.z; return c; }
/** * @param a * @param b * @return */ public static Point3D fois(Point3D a, double b) { Point3D c = new Point3D(a.x, a.x, a.x); c.x = a.x * b; c.y = a.y * b; c.z = a.z * b; return c; }
public boolean isEqual(Point3D other) { if ((Math.abs(Double.parseDouble(x) - Double.parseDouble(other.getX())) <= Epsilon) && (Math.abs(Double.parseDouble(y) - Double.parseDouble(other.getY())) <= Epsilon) && (Math.abs(Double.parseDouble(z) - Double.parseDouble(other.getZ())) <= Epsilon)) return true; return false; }
/* * Connects the letters by going along the Z axis */ public static void ConnectLetters(Graphics g, Object letter) { Point3D topPoint = new Point3D(0, 0, 0); for (int i = 0; i < letter.vertices.length; i++) { Move3D(letter.vertices[i], letter.aT, CAMERA); topPoint.SetCoords(letter.vertices[i].x, letter.vertices[i].y, -1); Draw3D(g, topPoint, letter.aT, CAMERA); } }
/* * Takes in an object and a height and connects all the points of the object * from the floor to the specified height */ public static void ConnectWalls(Graphics g, Object floorObject, double y) { Point3D topPoint = new Point3D(0, 0, 0); for (int i = 0; i < floorObject.vertices.length; i++) { Move3D(floorObject.vertices[i], floorObject.aT, CAMERA); topPoint.SetCoords(floorObject.vertices[i].x, y, floorObject.vertices[i].z); Draw3D(g, topPoint, floorObject.aT, CAMERA); } }
public static void main(String[] args) { Point3D p1 = new Point3D(1, 2, 3); Point3D p2 = new Point3D(1, 2, 3); System.out.println(p1); System.out.println(p2); System.out.println("p1==p2?" + (p1 == p2)); System.out.println("p1.equals(p2)?" + (p1.equals(p2))); }
@Override public float distanceFromCameraToPoint(Point3D point3D) { float distance; Point3D fromCoordinates = cameraSettings.getRealFromCoordinates(); distance = fromCoordinates.distanceTo(point3D); return distance; }
private static void DoConvertDMS2D(String pointFile, String saveFile) { FileReader in = null; BufferedReader reader = null; int cntLine = 0; FileWriter out = null; BufferedWriter writer = null; try { out = new FileWriter(saveFile); } catch (IOException e) { e.printStackTrace(); } writer = new BufferedWriter(out); try { in = new FileReader(pointFile); reader = new BufferedReader(in); String str; Point3D pt3d = null; while (true) { str = reader.readLine(); if (str == null) break; cntLine++; pt3d = Point3D.Create(str); if (cntLine % 1000000 == 0) { System.out.println("cnt : " + (cntLine)); writer.flush(); } if (pt3d != null) { writer.write( String.format("%.9f", pt3d.GetX()) + " " + String.format("%.9f", pt3d.GetY()) + " " + String.format("%.3f", pt3d.GetZ())); writer.newLine(); } else { System.out.println("Err : " + str); } } System.out.println("Lines : " + cntLine); } catch (Exception e) { e.printStackTrace(); } finally { try { in.close(); writer.flush(); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
public Point3D normal() { if (n == null) { Point3D a = new Point3D(p1.x - p0.x, p1.y - p0.y, p1.z - p0.z); Point3D b = new Point3D(p2.x - p0.x, p2.y - p0.y, p2.z - p0.z); n = a.cross(b).normalize(); } return n; }
public void setDestination(Planet destination, Point3D pos) { this.destination = destination; this.pos1.set(pos.x, pos.y, pos.z); pos4.set(destination.x, 2 * destination.size, -3 * destination.size); pos2 = new Point3D(pos1.x, destination.size, pos.z - 500); pos3 = new Point3D(pos4.x, destination.size, -500); elapsedTime = 0; returnPos.set(pos.x, pos.y, pos.z); }
public static void main(String[] args) { // Point Construction Point3D p1 = new Point3D(1, 2, 4); Point3D p2 = new Point3D(2, 1, 5); // Output System.out.println(p2.distance(p1)); System.out.println(p2.distanceL1(p1)); }
/** * Translates the plane by a vector <code>translateVector</code> without changing the original * plane. * * @param translateVector the point or vector by which the object shall be translated * @return the resulting translated plane */ public Plane3D translateBy(Point3D translateVector) { double additionalDist = normal.getScalarProduct(translateVector); double newDist = distance + additionalDist; Point3D newDir; if (newDist < 0) { newDir = normal.reflectOrigin(); newDist = -newDist; } else { newDir = normal; } return new Plane3D(newDist, newDir); }
/** Returns XYZ coordinates of the point. Z will be set to 0 if Z is missing. */ Point3D getXYZ() { if (isEmptyImpl()) throw new GeometryException("This operation should not be performed on an empty geometry."); Point3D pt = new Point3D(); pt.x = m_attributes[0]; pt.y = m_attributes[1]; if (m_description.hasZ()) pt.z = m_attributes[2]; else pt.z = VertexDescription.getDefaultValue(Semantics.Z); return pt; }
/* * Draws and labels the axes */ public static void DrawAxes(Graphics g) { String num = ""; Point2D tmpPoint = new Point2D(0, 0); drawPoint drawer = new drawPoint(0, 0); // X axis Point3D PointToDraw = new Point3D(curWin.RightX, 0, 0); Move3D(PointToDraw, IDENTITY, CAMERA); PointToDraw.SetCoords(curWin.LeftX, 0, 0); Draw3D(g, PointToDraw, IDENTITY, CAMERA); for (double i = curWin.LeftX; i <= curWin.RightX; i += 0.5) { if (i == 0.0) continue; PointToDraw.SetCoords(i, 0, 0); num = "" + i; Move3D(PointToDraw, IDENTITY, CAMERA); drawer = ViewPortToFrameWindow(curPos); g.drawString(num, drawer.x, drawer.y); } // y-axis PointToDraw.SetCoords(0, curWin.TopY - 1, 0); Move3D(PointToDraw, IDENTITY, CAMERA); PointToDraw.SetCoords(0, curWin.BotY, 0); Draw3D(g, PointToDraw, IDENTITY, CAMERA); for (double i = curWin.BotY; i <= curWin.TopY - 1; i += 0.5) { if (i == 0.0) continue; PointToDraw.SetCoords(0.1, i, 0); num = "" + i; Move3D(PointToDraw, IDENTITY, CAMERA); drawer = ViewPortToFrameWindow(curPos); g.drawString(num, drawer.x, drawer.y); } // z-axis PointToDraw.SetCoords(0, 0, 4); // 4 is to keep it on the screen Move3D(PointToDraw, IDENTITY, CAMERA); PointToDraw.SetCoords(0, 0, -4); // keep it on the screen Draw3D(g, PointToDraw, IDENTITY, CAMERA); for (double i = -4.0; i <= 4.0; i += 0.5) { if (i == 0.0) continue; PointToDraw.SetCoords(0, 0.1, i); num = "" + i; Move3D(PointToDraw, IDENTITY, CAMERA); drawer = ViewPortToFrameWindow(curPos); g.drawString(num, drawer.x, drawer.y); } }
protected void drawWAll(Color c, IDrawOperation op, Point3D[] wall, int dx, int dy, int dz) { Point3D a = new Point3D(0, 0, 0); Point3D b = new Point3D(0, 0, 0); int[] xs = new int[4]; int[] ys = new int[4]; for (int i = 0; i < 4; i++) { int j = (i == 3) ? 0 : i + 1; a.x = wall[i].x + dx; a.y = wall[i].y + dy; a.z = wall[i].z + dz; b.x = wall[j].x + dx; b.y = wall[j].y + dy; b.z = wall[j].z + dz; checkPoint(a); checkPoint(b); xs[i] = sx + a.x; ys[i] = sy - a.y; } op.fillPolygone(c, xs, ys, 1.0f); op.drawPolygone(Color.black, xs, ys, 1.0f); }
/** * Checks if a Plane is close or equal to another one. * * @param geom the Geometry3D which the object should be compared to * @return <code>true</code> if the other plane is close or equal */ public boolean isSimilar(Plane3D geom) { Plane3D s = (Plane3D) geom; if (Utilities3D.isEqual(distance, s.getDistance())) { if (normal.isSimilar(s.getNormalVec())) { return true; } else if (Utilities3D.isEqual(distance, 0.0) && normal.isSimilar(s.getNormalVec().reflectOrigin())) { return true; } else { return false; } } else { return false; } }
Point3D convertFor(Point3D other) { Point3D alt0 = new Point3D(this.x, this.y, this.z); Point3D alt1 = new Point3D(this.x + World.mapWidth(), this.y, this.z); Point3D alt2 = new Point3D(this.x - World.mapWidth(), this.y, this.z); double d0 = Point3D.euclideanDistance(alt0, other); double d1 = Point3D.euclideanDistance(alt1, other); double d2 = Point3D.euclideanDistance(alt2, other); if (d0 <= d1 && d0 <= d2) { return alt0; } else if (d1 <= d0 && d1 <= d2) { return alt1; } else { return alt2; } }
/** * Constructs a 3D Plane. The location of the plane is determined by <code>distance</code>, the * minimal distance of the plane from the origin. * * @param distance the distance of the plane from the origin * @param normal The (direction of the) unit normal vector */ public Plane3D(double distance, Point3D normal) { this.distance = distance; this.normal = normal.norm(); if (distance < 0.0) { throw new IllegalArgumentException("Cant work with negative distance!"); } }
@Override public Point3D convertToCameraCoordinateSystem(Point3D point3D) { Point3D converted = super.convertToCameraCoordinateSystem(point3D); double focalLength = cameraSettings.getFocalLength(); double ratio = focalLength / converted.z; double x = ratio * converted.x; double y = ratio * converted.y; double z = converted.z; converted.x = x; converted.y = y; converted.z = z; return converted; }
private void checkPoint(Point3D p) { // double cos = Math.cos(Math.PI/8); // double sin = Math.sin(Math.PI/8); // Point3D p2 = new Point3D(0,0,0); // sur l'axe z // p2.x = (int) (p.x*cos+p.y*sin); // p2.y = (int) (-p.x*sin+p.y*cos); // p2.z = p.z; // vx = (int) (pointFuite.x*cos+pointFuite.y*sin); // vy = (int) (-pointFuite.x*sin+pointFuite.y*cos); // vz = (int) (pointFuite.z); // p2.x = (int) (p.x*cos-p.z*sin); // p2.y = (int) (p.y); // p2.z = (int) (-p.x*sin+p.z*cos); // p2.x = (int) (p.x); // p2.y = (int) (p.y*cos-p.z*sin); // p2.z = (int) (p.y*sin+p.z*cos); // vx = (int) (pointFuite.x); // vy = (int) (pointFuite.y*cos-pointFuite.z*sin); // vz = (int) (pointFuite.y*sin+pointFuite.z*cos); // double a = (vy-p2.y) / (vx-p2.x); // double z = Math.min(vz, p2.z); // // double var = z / vz; // double yy = 1 - 1/(1+var*1.5); // double d = yy * (vx-p2.x); double a = (vy - p.y) / (vx - p.x); double z = Math.min(vz, p.z); double var = z / vz; double yy = 1 - 1 / (1 + var * 1.5); double d = yy * (vx - p.x); // p.x = p2.x; // p.y = p2.y; // p.z = p2.z; p.x += (int) d; p.y += (int) (a * d); }
public void setVertexAt(int index, Point3D v) { Point3D vertex = vertices.get(index); // System.out.println("original: " + vertex); // System.out.println("new: " + v); vertex.setX(v.getX()); vertex.setY(v.getY()); vertex.setZ(v.getZ()); }
/* * Plots the given equation */ public static void PlotGraph(Graphics g) { // start at the low point Point3D firstPoint = new Point3D(-1.25, -1.25, 0); Point3D secondPoint = new Point3D(-1.25, -1.25, 0); double inc = 0.05; // this looked the best g.setColor(Color.green); for (double y = -1.25; y <= 1.25; y += inc) { for (double x = -1.25; x <= 1.25; x += inc) { // draw the small square firstPoint.SetCoords(x, y, 0); secondPoint.SetCoords(x + inc, y + inc, 0); DrawSquare(g, firstPoint, secondPoint); } } g.setColor(Color.black); DrawAxes(g); Branding(g, "z = (x^2) + (y^2) - (x^3) - 8*x*(y^4)"); }
/** * Scales the plane by a factor. * * @param scale the scale for this operation * @return the scaled object */ public Plane3D scaleBy(double scale) { double newDist = distance * scale; Point3D newDir; if (newDist < 0) { newDir = normal.reflectOrigin(); newDist = -newDist; } else { newDir = normal; } return new Plane3D(newDist, newDir); }
/* * draws a square for the graph */ public static void DrawSquare(Graphics g, Point3D leftPoint, Point3D rightPoint) { Point3D PointToDraw = new Point3D(leftPoint.x, leftPoint.y, PlotFunction(leftPoint.x, leftPoint.y)); Move3D(PointToDraw, IDENTITY, CAMERA); // leftX -> rightX PointToDraw.SetCoords(rightPoint.x, leftPoint.y, PlotFunction(rightPoint.x, leftPoint.y)); Draw3D(g, PointToDraw, IDENTITY, CAMERA); // botY -> topY PointToDraw.SetCoords(rightPoint.x, rightPoint.y, PlotFunction(rightPoint.x, rightPoint.y)); Draw3D(g, PointToDraw, IDENTITY, CAMERA); // rightX -> leftX PointToDraw.SetCoords(leftPoint.x, rightPoint.y, PlotFunction(leftPoint.x, rightPoint.y)); Draw3D(g, PointToDraw, IDENTITY, CAMERA); // topY -> botY PointToDraw.SetCoords(leftPoint.x, leftPoint.y, PlotFunction(leftPoint.x, leftPoint.y)); Draw3D(g, PointToDraw, IDENTITY, CAMERA); }