/* Une classe de stockage de position dans un espace 2D */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2009 */
public class Position2D {
public float x;
public float y;
public float t;
public Position2D() {
x = y = 0.0F;
t = 1.0F;
}
public Position2D(float x,float y) {
this.x = x;
this.y = y;
this.t = 1.0F;
}
public Position2D(Position2D p) {
x = p.x;
y = p.y;
t = 1.0F;
}
public String toString() {
return("["+x+","+y+"]");
}
}
/* Une classe stockage de segment de droite */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2009 */
public class Segment {
public Position2D pi;
public Position2D pf;
public Segment() {
pi = new Position2D();
pf = new Position2D();
}
public Segment(float xi,float yi,float xf,float yf) {
pi = new Position2D(xi,yi);
pf = new Position2D(xf,yf);
}
public Segment(Position2D pi,Position2D pf) {
this.pi = new Position2D(pi);
this.pf = new Position2D(pf);
}
public Segment(Segment s) {
pi = new Position2D(s.pi);
pf = new Position2D(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+"]");
}
}
/* Une classe de stockage de rectangle */
/* a bords horizontaux et verticaux */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2009 */
public class Rectangle {
public Position2D ig;
public Position2D sd;
public Rectangle() {
ig = new Position2D();
sd = new Position2D();
}
public Rectangle(float xmin,float ymin,float xmax,float ymax) {
ig = new Position2D(xmin,ymin);
sd = new Position2D(xmax,ymax);
}
public Rectangle(Position2D ig,Position2D sd) {
this.ig = new Position2D(ig);
this.sd = new Position2D(sd);
}
public Rectangle(Rectangle r) {
ig = new Position2D(r.ig);
sd = new Position2D(r.sd);
}
private int codeZone(Position2D p) {
int cz = ( p.x < ig.x ) ? 0x01 : 0x00;
if ( p.x > sd.x )
cz += 0x02;
if ( p.y < ig.y )
cz += 0x04;
else
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 ) {
int c = ci;
ci = cf;
cf = c;
Position2D p = s.pi;
s.pi = s.pf;
s.pf = p; }
if ( (ci & 0x01) != 0 ) {
s.pi.y = s.ordonnee(ig.x);
s.pi.x = ig.x; }
else
if ( (ci & 0x02) != 0 ) {
s.pi.y = s.ordonnee(sd.x);
s.pi.x = sd.x; }
else
if ( (ci & 0x04) != 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+"]");
}
}
/* Une classe application de test de l'algorithme */
/* de Cohen-Sutherland */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2009 */
public class CohenSutherland {
public static void main(String [] args) {
Segment s = new Segment(new Position2D(1.0F,2.0F),
new Position2D(8.0F,6.0F));
Rectangle r = new Rectangle(new Position2D(2.0F,3.0F),
new Position2D(6.0F,5.0F));
Segment sc = r.clip(s);
if ( sc != null )
System.out.println(sc);
}
}
/* Une classe application de test de l'algorithme */
/* de Cohen-Sutherland */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2009 */
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);
}
}
/* Une classe heritant de Frame pour implanter */
/* la fenetre de l'application de test */
/* de l'algorithme de Cohen-Sutherland */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2009 */
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 Position2D pi = new Position2D(50.0F,70.0F);
private Position2D pf = new Position2D(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 Position2D(80.0F,110.0F),
new Position2D(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 Position2D(e.getX(),e.getY());
repaint(); }
if ( e.getButton() == MouseEvent.BUTTON3 ) {
pf = new Position2D(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 Position2D(e.getX(),e.getY());
repaint(); }
if ( e.getButton() == MouseEvent.BUTTON3 ) {
pf = new Position2D(e.getX(),e.getY());
repaint(); }
}
public void mouseReleased(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
if ( (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0 ) {
pi = new Position2D(e.getX(),e.getY());
repaint(); }
if ( (e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0 ) {
pf = new Position2D(e.getX(),e.getY());
repaint(); }
}
public void mouseMoved(MouseEvent e) {
}
}