001package scene; 002 003import java.util.Random; 004import java.awt.Color; 005 006/** 007 A Vertex object has three doubles which represent the coordinates 008 of a point in 3-dimensional space. 009 010 When a Vertex object is created in a client program, before the 011 Vertex object moves down the graphics rendering pipeline, the 012 coordinates in the Vertex will be in "camera coordinates". 013 014 As a Vertex object moves down the graphics rendering pipeline, 015 the coordinates in the Vertex will be transformed from one 016 coordinate system to another. 017 018 A vertex object also has a color (or "shade"). The color of a 019 line segment is interpolated from the colors at its two endpoints. 020*/ 021public class Vertex 022{ 023 public double x, y, z; 024 public double r = 1.0, g = 1.0, b = 1.0; // color at this vertex 025 026 027 /** 028 Construct a default vertex. 029 */ 030 public Vertex() 031 { 032 set(0.0, 0.0, 0.0); 033 } 034 035 036 /** 037 Construct a new vertex using the given x, y, and z coordinates. 038 039 @param x x-coordinate of the new Vertex 040 @param y y-coordinate of the new Vertex 041 @param z z-coordinate of the new Vertex 042 */ 043 public Vertex(double x, double y, double z) 044 { 045 set(x, y, z); 046 } 047 048 049 /** 050 Construct a new vertex with the given coordinates 051 and the given color. 052 053 @param x x-coordinate of the new Vertex 054 @param y y-coordinate of the new Vertex 055 @param z z-coordinate of the new Vertex 056 @param c Color of the new Vertex 057 */ 058 public Vertex(double x, double y, double z, Color c) 059 { 060 set(x, y, z); 061 setColor(c); 062 } 063 064 065 /** 066 Construct a new vertex that is a copy of another vertex. 067 068 @param v Vertex to make a copy of 069 */ 070 public Vertex(Vertex v) 071 { 072 set(v.x, v.y, v.z); 073 setColor(v); 074 } 075 076 077 /** 078 Set the coordinates of this vertex. 079 080 @param x new x-coordinate for this Vertex 081 @param y new y-coordinate for this Vertex 082 @param z new z-coordinate for this Vertex 083 */ 084 public void set(double x, double y, double z) 085 { 086 this.x = x; 087 this.y = y; 088 this.z = z; 089 } 090 091 092 /** 093 Get the color of this vertex. 094 095 @return the color of this Vertex object 096 */ 097 public Color getColor() 098 { 099 return new Color((float)this.r, (float)this.g, (float)this.b); 100 } 101 102 103 /** 104 Set the color of this vertex using doubles between 0 and 1. 105 106 @param r red color value for this Vertex as a double between 0 and 1 107 @param g green color value for this Vertex as a double between 0 and 1 108 @param b blue color value for this Vertex as a double between 0 and 1 109 */ 110 public void setColor(double r, double g, double b) 111 { 112 this.r = r; 113 this.g = g; 114 this.b = b; 115 116 if ( (r < 0.0) || (r > 1.0) 117 || (g < 0.0) || (g > 1.0) 118 || (b < 0.0) || (b > 1.0) ) 119 { 120 System.err.println("ERROR! Invalid double color for vertex"); 121 System.err.println( this.toString() ); 122 System.err.printf("<r,g,b> = <% .5f % .5f % .5f>\n", r, g, b); 123 System.exit(-1); 124 } 125 } 126 127 128 /** 129 Set the color of this vertex using ints between 0 and 255. 130 131 @param r red color value for this Vertex as an integer between 0 and 255 132 @param g green color value for this Vertex as an integer between 0 and 255 133 @param b blue color value for this Vertex as an integer between 0 and 255 134 */ 135 public void setColor(int r, int g, int b) 136 { 137 this.r = ((float)r)/(float)255; 138 this.g = ((float)g)/(float)255; 139 this.b = ((float)b)/(float)255; 140 141 if ( (r < 0) || (r > 255) 142 || (g < 0) || (g > 255) 143 || (b < 0) || (b > 255)) 144 { 145 System.err.println("ERROR! Invalid int color for vertex"); 146 System.err.println( this.toString() ); 147 System.err.printf("<r,g,b> = <%d %d %d>\n", r, g, b); 148 System.exit(-1); 149 } 150 } 151 152 153 /** 154 Set the color of this vertex using a Color object. 155 156 @param c Color for this Vertex object 157 */ 158 public void setColor(Color c) 159 { 160 setColor(c.getRed(), c.getGreen(), c.getBlue()); 161 } 162 163 164 /** 165 Set the color of this vertex using the colors from another vertex. 166 167 @param v Vertex object to get color values from 168 */ 169 public void setColor(Vertex v) 170 { 171 // copy the color from v to this vertex 172 setColor(v.r, v.g, v.b); 173 } 174 175 176 /** 177 Set the color of this vertex to a random color. 178 */ 179 public void setColorRandom() 180 { 181 Random generator = new Random(); 182 double r = generator.nextDouble(); 183 double g = generator.nextDouble(); 184 double b = generator.nextDouble(); 185 setColor(r, g, b); 186 } 187 188 189 /** 190 For debugging. 191 192 @return String representation of this Vertex object 193 */ 194 public String toString() 195 { 196 // Here is one way to get programmable precision and width. 197 int p = 5; // the precision for the following format string 198 int t = p + 4; // the width for the following format string 199 String format = "(x,y,z) = (% "+t+"."+p+"f % "+t+"."+p+"f % "+t+"."+p+"f)\n"; 200 return String.format( format, x, y, z); 201 //return String.format("(x,y,z) = (% .5f % .5f % .5f)", x, y, z); 202 } 203}