/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2006 */
public class Position {
public float x;
public float y;
public float t;
public Position() {
x = y = 0.0F;
t = 1.0F;
}
public Position(float x,float y) {
this.x = x;
this.y = y;
this.t = 1.0F;
}
public Position(Position p) {
x = p.x;
y = p.y;
t = 1.0F;
}
public String toString() {
return("["+x+","+y+"]");
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2006 */
public class Segment {
public Position pi;
public Position pf;
public Segment() {
pi = new Position();
pf = new Position();
}
public Segment(float xi,float yi,float xf,float yf) {
pi = new Position(xi,yi);
pf = new Position(xf,yf);
}
public Segment(Position pi,Position pf) {
this.pi = new Position(pi);
this.pf = new Position(pf);
}
public Segment(Segment s) {
pi = new Position(s.pi);
pf = new Position(s.pf);
}
public float abscisse(float y) {
if ( pf.x != pi.x ) {
float a = (pf.y-pi.y) / (pf.x-pi.x);
float b = pi.y - a*pi.x;
return((y-b)/a); }
else
return(pf.x);
}
public float ordonnee(float x) {
float a = (pf.y-pi.y) / (pf.x-pi.x);
float b = pi.y - a*pi.x;
return(a*x+b);
}
public String toString() {
return("["+pi+","+pf+"]");
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2006 */
public class Rectangle {
public Position ig;
public Position sd;
public Rectangle() {
ig = new Position();
sd = new Position();
}
public Rectangle(float xmin,float ymin,float xmax,float ymax) {
ig = new Position(xmin,ymin);
sd = new Position(xmax,ymax);
}
public Rectangle(Position ig,Position sd) {
this.ig = new Position(ig);
this.sd = new Position(sd);
}
public Rectangle(Rectangle r) {
ig = new Position(r.ig);
sd = new Position(r.sd);
}
private int codeZone(Position p) {
int cz = ( p.x < ig.x ) ? 0x01 : 0x00;
if ( p.x > sd.x )
cz += 0x02;
if ( p.y < ig.y )
cz += 0x04;
if ( p.y > sd.y )
cz += 0x08;
return(cz);
}
public Segment clip(Segment s) {
s = new Segment(s);
int ci = codeZone(s.pi);
int cf = codeZone(s.pf);
while ( ( ( ci != 0 ) || ( cf !=0 ) ) && ( (ci&cf) == 0 ) ) {
if ( ci == 0 ) {
ci = cf;
cf = 0;
Position p = s.pi;
s.pi = s.pf;
s.pf = p; }
if ( (ci & 0x0001) != 0 ) {
s.pi.y = s.ordonnee(ig.x);
s.pi.x = ig.x; }
else
if ( (ci & 0x0002) != 0 ) {
s.pi.y = s.ordonnee(sd.x);
s.pi.x = sd.x; }
else
if ( (ci & 0x0004) != 0 ) {
s.pi.x = s.abscisse(ig.y);
s.pi.y = ig.y; }
else {
s.pi.x = s.abscisse(sd.y);
s.pi.y = sd.y; }
ci = codeZone(s.pi); }
return((ci == 0 ) ? s : null);
}
public String toString() {
return("["+ig+","+sd+"]");
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2006 */
public class CohenSutherland {
public static void main(String [] args) {
Segment s = new Segment(new Position(1.0F,2.0F),
new Position(8.0F,6.0F));
Rectangle r = new Rectangle(new Position(2.0F,3.0F),
new Position(6.0F,5.0F));
Segment sc = r.clip(s);
if ( sc != null )
System.out.println(sc);
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2006 */
import java.awt.*;
public class CohenSutherlandJava2D {
public static void main(String [] args) {
CSFrame f = new CSFrame("Cohen-Sutherland");
f.setSize(320,240);
f.setLocation(50,250);
f.setVisible(true);
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2006 */
import java.awt.*;
import java.awt.event.*;
public class CSFrame extends Frame
implements WindowListener,
ActionListener,
MouseListener,
MouseMotionListener {
private MenuBar mb = new MenuBar();
private Menu m1 = new Menu("Fichier");
private MenuItem mq = new MenuItem("Quitter");
private Position pi = new Position(50.0F,70.0F);
private Position pf = new Position(270.0F,200.0F);
public CSFrame(String s) {
super();
setTitle(s);
addWindowListener(this);
addMouseListener(this);
addMouseMotionListener(this);
setMenuBar(mb);
mb.add(m1);
m1.add(mq);
mq.addActionListener(this);
}
public void paint(Graphics g) {
Segment s = new Segment(pi,pf);
Rectangle r = new Rectangle(new Position(80.0F,110.0F),
new Position(250.0F,170.0F));
g.drawLine((int) s.pi.x,(int) s.pi.y,
(int) s.pf.x,(int) s.pf.y);
g.drawRect((int) r.ig.x,(int) r.ig.y,
(int) (r.sd.x-r.ig.x),(int) (r.sd.y-r.ig.y));
Segment sc = r.clip(s);
if ( sc != null ) {
g.setColor(new Color(0.0F,0.0F,1.0F));
g.drawLine((int) sc.pi.x,(int) sc.pi.y,
(int) sc.pf.x,(int) sc.pf.y); }
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
quitter();
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public void quitter() {
System.exit(0);
}
public void actionPerformed(ActionEvent e) {
if ( e.getSource() == mq ) {
quitter(); }
}
public void mouseClicked(MouseEvent e) {
if ( e.getButton() == MouseEvent.BUTTON1 ) {
pi = new Position(e.getX(),e.getY());
repaint(); }
if ( e.getButton() == MouseEvent.BUTTON3 ) {
pf = new Position(e.getX(),e.getY());
repaint(); }
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if ( e.getButton() == MouseEvent.BUTTON1 ) {
pi = new Position(e.getX(),e.getY());
repaint(); }
if ( e.getButton() == MouseEvent.BUTTON3 ) {
pf = new Position(e.getX(),e.getY());
repaint(); }
}
public void mouseReleased(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
if ( (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0 ) {
pi = new Position(e.getX(),e.getY());
repaint(); }
if ( (e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0 ) {
pf = new Position(e.getX(),e.getY());
repaint(); }
}
public void mouseMoved(MouseEvent e) {
}
}