package myGraphics.Shape3d; import java.awt.Graphics; import java.awt.Color; import myGraphics.VectorGrafix.*; import myGraphics.Perspective.*; public class Shape { public Path crossSection; public Path profile; public Vector3d refPoint; // (obsolete in ) tracks current position of shape public Transformation3d trans; // current transformation of shape relative to default public Color crossColor; public Color proColor; public Shape() { crossSection = new Path(); profile = new Path(); refPoint = new Vector3d(0.0, 0.0, 0.0); trans = new Transformation3d(); // identity matrix crossColor = Color.red; proColor = Color.blue; } public Shape(Shape sourceShape) { crossSection = sourceShape.crossSection.copy(); profile = sourceShape.profile.copy(); refPoint = sourceShape.refPoint.copy(); trans = sourceShape.trans.copy(); crossColor = sourceShape.crossColor; proColor = sourceShape.proColor; } public Shape copy() { Shape target = new Shape(); target.crossSection = crossSection.copy(); target.profile = profile.copy(); target.refPoint = refPoint.copy(); target.trans = trans.copy(); target.crossColor = crossColor; target.proColor = proColor; return target; } public void transformShape(Transformation3d new_trans) { Vector3d temp = refPoint.negate(); Transformation3d to_origin = new Transformation3d(); to_origin = to_origin.translate(temp.x(), temp.y(), temp.z()); Transformation3d and_back = new Transformation3d(); and_back = and_back.translate(refPoint.x(), refPoint.y(), refPoint.z()); new_trans = new Transformation3d(new_trans.multiply(to_origin)); new_trans = new Transformation3d(and_back.multiply(new_trans)); refPoint = new_trans.apply(refPoint); trans = new Transformation3d(new_trans.multiply(trans)); } protected Vector3d transformPoint(Vector3d target) { return trans.apply(target); } public void displayShape(Graphics g, Camera theCamera, Viewport theViewport) { Vector3d point1 = new Vector3d(); Vector3d point2 = new Vector3d(); int i = 0, j = 0; Color origColor = g.getColor(); g.setColor(crossColor); profile.start(); while (profile.next()) { double z = profile.x(); double scale = profile.y(); crossSection.start(); for (i = 0; crossSection.next(); i++) { if (i > 0) { point1 = point2.copy(); } point2 = new Vector3d(scale * crossSection.x(), scale * crossSection.y(), z); point2 = transformPoint(point2); if (i > 0) { theCamera.displayLine(g, theViewport, point1, point2); } } } g.setColor(proColor); crossSection.start(); while (crossSection.next()) { double x = crossSection.x(); double y = crossSection.y(); profile.start(); for (i = 0; profile.next(); i++) { double z = profile.x(); double scale = profile.y(); if (i > 0) { for (j = 0; j < 3; j++) { point1.set_term(j, point2.get_term(j)); } } point2 = new Vector3d(scale * x, scale * y, z); point2 = transformPoint(point2); if (i > 0) { theCamera.displayLine(g, theViewport, point1, point2); } } } g.setColor(origColor); } }