source: trunk/src/org/expeditee/items/Constraint.java@ 284

Last change on this file since 284 was 97, checked in by ra33, 16 years ago

Lots of changes!!

File size: 2.9 KB
Line 
1package org.expeditee.items;
2
3/**
4 * This class represents a constraint between two Dots on the screen. A vertical
5 * constraint means the Dot's Y values will be equal, likewise a horizontal
6 * constraint means the Dot's X values will be equal.
7 *
8 * @author jdm18
9 *
10 */
11public class Constraint {
12 public static final int VERTICAL = 2;
13
14 public static final int HORIZONTAL = 3;
15
16 public static final int DIAGONAL_POS = 4;
17
18 public static final int DIAGONAL_NEG = 5;
19
20 // the list of points that are constrained
21 private Item _start = null;
22
23 private Item _end = null;
24
25 // id and type info
26 private int _id;
27
28 private int _type;
29
30 /**
31 * Constructs a constraint with the two given end points, id, and type. Type
32 * should be one of the constants defined in this class.
33 *
34 * @param a
35 * One of the points in this constraint.
36 * @param b
37 * The other point in this constraint.
38 * @param id
39 * The ID of this constraint item.
40 * @param type
41 * The type of this constraint (horizontal, vertical).
42 */
43 public Constraint(Item a, Item b, int id, int type) {
44 _start = a;
45 _end = b;
46
47 a.addConstraint(this);
48 b.addConstraint(this);
49
50 _id = id;
51 _type = type;
52 }
53
54 /**
55 * @return The Item ID of this Constraint.
56 */
57 public int getID() {
58 return _id;
59 }
60
61 /**
62 *
63 * @return The type of this Constraint. The Type corresponds to the
64 * constants defined in this class.
65 */
66 public int getType() {
67 return _type;
68 }
69
70 public Item getStart() {
71 return _start;
72 }
73
74 public Item getEnd() {
75 return _end;
76 }
77
78 public boolean contains(Item i) {
79 if (_start == i || _end == i)
80 return true;
81
82 return false;
83 }
84
85 /**
86 * If there are only two points in this constraint, this method returns the
87 * Point not passed to it. If there are more than two points then the first
88 * encountered is returned.
89 *
90 * @return A Point in the constraint that is not the one given (or null if
91 * none is found).
92 */
93 public Item getOppositeEnd(Item from) {
94 if (from == _start)
95 return _end;
96
97 if (from == _end)
98 return _start;
99
100 return null;
101 }
102
103 public void replaceEnd(Item toReplace, Item with) {
104 assert(_start != _end);
105 if (_start == toReplace) {
106 _start = with;
107 } else if (_end == toReplace) {
108 _end = with;
109 }
110 toReplace.removeConstraint(this);
111 with.addConstraint(this);
112 }
113
114 public String getLineEnds() {
115 String ends = "";
116
117 if (_start != null)
118 ends += _start.getID();
119
120 if (_end != null)
121 ends += " " + _end.getID();
122
123 return ends;
124 }
125
126 public boolean isDiagonal() {
127 return _type == Constraint.DIAGONAL_NEG || _type == Constraint.DIAGONAL_POS;
128 }
129
130 public Float getGradient() {
131 switch(_type){
132 case Constraint.DIAGONAL_NEG:
133 return -1.0F;
134 case Constraint.DIAGONAL_POS:
135 return 1.0F;
136 case Constraint.HORIZONTAL:
137 return 0.0F;
138 case Constraint.VERTICAL:
139 return Float.POSITIVE_INFINITY;
140 }
141 assert(false);
142 return null;
143 }
144
145}
Note: See TracBrowser for help on using the repository browser.