Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/home/landauf/painter/miniproject/image/clip.cpp

Go to the documentation of this file.
00001 //
00002 //    Title: clip.cc
00003 //  Created: Thu Nov  1 17:23:43 2001
00004 //   Author: Tim Weyrich <weyrich@inf.ethz.ch>
00005 //
00006 // copyright (c) 2001--2003, Computer Graphics Laboratory, ETH Zuerich
00007 //
00008 
00009 #include <math.h>
00010 
00011 #include "clip.h"
00012 
00014 
00015 int clippingCode(double x, double y, double xcl1, double ycl1, double xcl2, double ycl2)
00016 {
00017   //   Compute the endpoint codes for clip().
00018   int code = 0;
00019   if (x < xcl1) code = code | 0x1;
00020   if (x > xcl2) code = code | 0x2;
00021   if (y < ycl1) code = code | 0x4;
00022   if (y > ycl2) code = code | 0x8;
00023   return code;
00024 }
00025 
00026 int clip(float *x, float *y, float xclipl, float yclipb, float xclipr, float yclipt)
00027 {
00028   //   Clipping routine: Cohen Sutherland algorithm.
00029   // If Clip ==2 the segment is outside the boundary.
00030   // If Clip ==1 the segment has one point outside the boundary.
00031   // If Clip ==0 the segment is inside the boundary.
00032   //
00033   // _Input parameters:
00034   //
00035   //  x[2], y[2] : Segment coordinates
00036   //  xclipl, yclipb, xclipr, yclipt : Clipping boundary
00037   //
00038   // _Output parameters:
00039   //
00040   //  x[2], y[2] : New segment coordinates
00041   //
00042   const float P=10000;
00043   int clip = 0;
00044 
00045   for (int i=0;i<2;i++) {
00046     if (fabs(xclipl-x[i]) <= fabs(xclipr-xclipl)/P) x[i] = xclipl;
00047     if (fabs(xclipr-x[i]) <= fabs(xclipr-xclipl)/P) x[i] = xclipr;
00048     if (fabs(yclipb-y[i]) <= fabs(yclipt-yclipb)/P) y[i] = yclipb;
00049     if (fabs(yclipt-y[i]) <= fabs(yclipt-yclipb)/P) y[i] = yclipt;
00050   }
00051 
00052   //Compute the first endpoint codes.
00053   int code1 = clippingCode(x[0],y[0],xclipl,yclipb,xclipr,yclipt);
00054   int code2 = clippingCode(x[1],y[1],xclipl,yclipb,xclipr,yclipt);
00055 
00056   double xt=0, yt=0;
00057   int clipped = 0; //this variable could be used in a future version
00058   while(code1 + code2) {
00059     clipped = 1;
00060 
00061     //The line lies entirely outside the clipping boundary
00062 
00063     if (code1&code2) {
00064       clip = 2;
00065       return clip;
00066     }
00067     //The line is subdivided into several parts
00068 
00069     int ic = code1;
00070     if (ic == 0) ic = code2;
00071     if (ic & 0x1) {
00072       yt = y[0] + (y[1]-y[0])*(xclipl-x[0])/(x[1]-x[0]);
00073       xt = xclipl;
00074     }
00075     if (ic & 0x2) {
00076       yt = y[0] + (y[1]-y[0])*(xclipr-x[0])/(x[1]-x[0]);
00077       xt = xclipr;
00078     }
00079     if (ic & 0x4) {
00080       xt = x[0] + (x[1]-x[0])*(yclipb-y[0])/(y[1]-y[0]);
00081       yt = yclipb;
00082     }
00083     if (ic & 0x8) {
00084       xt = x[0] + (x[1]-x[0])*(yclipt-y[0])/(y[1]-y[0]);
00085       yt = yclipt;
00086     }
00087     if (ic == code1) {
00088       x[0]  = xt;
00089       y[0]  = yt;
00090       code1 = clippingCode(xt,yt,xclipl,yclipb,xclipr,yclipt);
00091     } else {
00092       x[1]  = xt;
00093       y[1]  = yt;
00094       code2 = clippingCode(xt,yt,xclipl,yclipb,xclipr,yclipt);
00095     }
00096   }
00097   clip = clipped;
00098   return clip;
00099 }
00100 

Generated on Mon Jan 30 09:13:03 2006 for Painter Framework by doxygen1.2.18