Appendix J.

advertisement
110
Appendix J: Smart Objects Two Object Class Java Code
/*
Class:
ObjCons
Author:
Dustin Eggink
Date:
07/12/01, annotated.
Description: ObjCons is an abreviation for ObjectConstrained. Instances of ObjCons
will becalled in the main S/O applet. Therse instances are the objects
that are placed within the tableaux of Cassowary. ObjCons have X,Y
and Z components which can be constrained individually by the Cassowary
solver. This class will do two things: convert X,Y,Z components back
to S/O in the form of floats after being constrained in Cassowary
numbers, and provide some graphics functions for the floor plan display
in the S/O applet.
*/
package CassRem;
////////////////////////////////IMPORTS/////////////////////////
import java.awt.*;
import CassEggink.*;
////////////////////////////////////////////////////////////////
public class ObjCons
{
ClPoint center;
// Cassowary Point
float rx, ry, rz;
float sx, sy, sz;
float origX, origZ;
float pt1X, pt1Z;
float pt2X, pt2Z;
float[] caso = new float[3];
int Scl;
int Disp1;
int Disp2;
float m;
int b;
float t1, t2;
int X1, X2, Z1, Z2;
///////////////////////////////////////////////////////////////
public ObjCons(float x, float y, float z) {
center = new ClPoint(x, y, z);
cvt();
}
public ObjCons(int a) {
center = new ClPoint(0, 0, 0, a);
cvt();
}
/////VERY IMPORTANT FUNCTION- CONVERTS FROM CASSOWARY TO FLOAT NUMBERS///////
void cvt() {
sx = (float) center.X().value();
sy = (float) center.Y().value();
sz = (float) center.Z().value();
}
111
//////////////////////////////////////////////////////////////////////////////////////
public void place () {
cvt();
caso[0] = sx;
caso[1] = sy;
caso[2] = sz;
caso[3] = 0;
}
public void SetCenter(float x, float y, float z) {
center.SetXYZ(x, y, z);
cvt();
}
public ClVariable X() { return center.X(); }
public ClVariable Y() { return center.Y(); }
public ClVariable Z() { return center.Z(); }
public ClPoint CenterPt() { return center; }
public double CenterX() { return center.Xvalue(); }
public double CenterY() { return center.Yvalue(); }
public double CenterZ() { return center.Zvalue(); }
//////////////////////////////INTERFACE FUNCTION//////////////////////////////////////
public boolean Contains(int x, int z,
int Scl, int Disp1, int Disp2 float pt1X, float pt1Z, float pt2X, float pt2Z)
{
// This function is for selecting the
// individual parts of the floor plan.
// This probably could be coded a lot
// simpler, but it isn't here.
/* THE ARGUMENTS- x and z will be the screen coordinates of where the user clicks.
Scl is the Scale conversion to go from VRML units to pixels.
Disp1 and Disp2 are the number of Displacement pixels (horiz and vert)
to get the plan view in the middle of the applet from the edge.
pt1/2/X/Z will be grabbed from the coord node in VRML by the S/O applet at
runtime.*/
cvt();
X1 = (int)(Scl*(sx + pt1X) + Disp1);
Z1 = (int)(Scl*(sz + pt1Z) + Disp2);
X2 = (int)(Scl*(sx + pt2X) + Disp1);
Z2 = (int)(Scl*(sz + pt2Z) + Disp2);
// convert from Cassowary to floats
// now change the X and Z to floats to
// ints and scale up to match will be
// used in the draw function.
// two points to a line- beginning pt and end pt.
if ((Z1 == Z2)||(X1 == X2)) { t1 = X1; t2 = X2}
else {
m = (X1 - X2)/(Z1 - Z2);
b = (int)(Z1 - (m*X1));
t1 = (((z - b)/m));
t2 = (((z - b)/m));
}
//
//
//
//
//
//
//
// this instance for if the line is
// horizontal or vertical.
else, figure the slope.
m is the slope of the drawn line.
b is the Y intercept of the line.
t1 and t2 are the X coordinate of
the points on
the line between X1/X2 and Z1/Z2 with
z as the Y coordinate.
112
/* THE TESTS- below are a series of tests to determine if the point clicked by the user
are on a line of an object in the floor plan. the first test is to determine which
way the line travels from the X1/Z1 point (up and to the right, down and to the left
or the two other directions.) this test (direction) is the first line of code. the
second test (Zposition) is whether the z coordinate is between Z1 and Z2. the final
test (Xposition) is whether the x coordinate matches t, which was calculated above.
the second and third tests add or subtract 1 to allow the user to miss by a pixel in
either direction. if the click passes all three tests, true is returned.
*/
// the direction of this line is to the left and up (remember java counts pixels down
// and to the right.)
if
( ((X1 >= X2) && (Z1 >= Z2))
// direction
&& ( (z <= (Z1 + 1)) && (z >= (Z2 - 1)) &&
// Zposition
(x <= t1 + 1) && (x >= t2 - 1)) )
// Xposition
return true;
///////////////////////// direction- right and up
else if ( ((X1 <= X2) && (Z1 >= Z2))
&& ( (z <= (Z1 + 1)) && (z >= (Z2 - 1)) &&
(x >= t1 - 1) && (x <= t2 + 1)) )
return true;
/////////////////////// direction- left and down
else if ( ((X1 >= X2) && (Z1 <= Z2))
&& ( (z >= (Z1 - 1)) && (z <= (Z2 + 1)) &&
(x <= t1 + 1) && (x >= t2 - 1)) )
return true;
/////////////////////// direction- right and down
else if ( ((X1 <= X2) && (Z1 <= Z2))
&& ( (z >= (Z1 - 1)) && (z <= (Z2 + 1)) &&
(x >= t1 - 1) && (x <= t2 + 1)) )
return true;
return false;
// if none of these pass, return false.
}
///////////////////////////////////GRAPHIC FUNCTION///////////////////////////
public void draw(Graphics g, int Scl, int Disp1, int Disp2,
float pt1X, float pt1Z, float pt2X, float pt2Z)
{
// the method for drawing the plan
cvt();
X1 = (int)(Scl*(sx + pt1X) + Disp1);
Z1 = (int)(Scl*(sz + pt1Z) + Disp2);
X2 = (int)(Scl*(sx + pt2X) + Disp1);
Z2 = (int)(Scl*(sz + pt2Z) + Disp2);
g.drawLine(X1, Z1, X2, Z2);
}
///////////////////////////////////END////////////////////////////////////////
}
Download