Liang–Barsky algorithm: Difference between revisions

Content deleted Content added
No edit summary
Line 189:
return y2;
}
}
</source>
 
Alternative java code:
<source lang="java">
 
/**
* 2d clipping with the Liang-Barsky algorithm.
*
* @author Philip Diffenderfer
*
*/
public class Clipping
{
/**
* A point in 2d space.
*/
public class Point
{
public float x, y, z;
}
/**
* A clipping bounds in 2d space.
*/
public class Bounds
{
public float xmin, ymin, xmax, ymax;
}
/**
* Clips the given points against the given clipping bounds. If the line
* lies outside of the clipping bounds then false is returned. If the line
* exists atleast partially in the clipping bounds then the points are
* clipped and true is returned.
*
* @param s The starting point of the line.
* @param e The ending point of the line.
* @param clip The bounds to clip against.
* @return True if the line exists in the clipping bounds.
*/
public boolean clipLine(Point s, Point e, Bounds clip)
{
// t-entering is initialized at 0 (s) and t-leaving is initialized at 1 (e)
float t[] = {0, 1};
float dx = e.x - s.x;
// Try clipping line on left edge
if (clipt(-dx, s.x - clip.xmin, t)) return false;
// Try clipping line on right edge
if (clipt(dx, clip.xmax - s.x, t)) return false;
float dy = e.y - s.y;
// Try clipping line on top edge
if (clipt(-dy, s.y - clip.ymin, t)) return false;
// Try clipping line on bottom edge
if (clipt(dy, clip.ymax - s.y, t)) return false;
// Was the ending point clipped?
if (t[1] < 1) {
e.x = s.x + t[1] * dx;
e.y = s.y + t[1] * dy;
}
// Was the starting point clipped?
if (t[0] > 0) {
s.x = s.x + t[0] * dx;
s.y = s.y + t[0] * dy;
}
// Line has been clipped and atleast partially exists in clip
return true;
}
/**
* Clips a line against a given edge and updates a respective t value if the
* line is clipped by that edge. If the line is completely outside the edge
* then true is returned, else false is returned if atleast a part of the
* line is on the inside of the edge.
*
* @param denom
* The denominator to the parametric equation for solving for t.
* @param num
* The numerator to the parametric equation for solving for t.
* @param t
* The array of t values where t[0] is t-entering and t[1] is t-leaving.
* @return
* True if the line is completely outside of the given edge, false if
* the line is atleast partially inside the edge.
*/
private boolean clipt(float denom, float num, float[] t)
{
// Potentially entering intersection...
if (denom > 0) {
// Value of t at intersection
float ti = num / denom;
// t-entering and t-leaving crossover, reject line.
if (ti > t[1]) {
return true;
}
// New t-entering found.
else if (ti > t[0]) {
t[0] = ti;
}
}
// Potentially leaving intersection...
else if (denom < 0) {
// Value of t at intersection
float ti = num / denom;
// t-entering and t-leaving crossover, reject line.
if (ti < t[0]) {
return true;
}
// New t-leaving found.
else if (ti < t[1]) {
t[1] = ti;
}
}
// Line outside of edge, reject.
else if (num > 0) {
return true;
}
// Line is partially or entirely inside of edge.
return false;
}
 
}
</source>