+ All Categories
Home > Documents > Laborator geometrie computationala

Laborator geometrie computationala

Date post: 28-Apr-2015
Category:
Upload: belciu-andra
View: 54 times
Download: 2 times
Share this document with a friend
Description:
geometrie computationala
122
1 Laborator 1 Se da un poligon simplu (nu are autointersectii). Pentru un punct M dat se cere sa se determine daca M apartine interiorului poligonului sau M apartine exteriorului poligonului sau M apartine frontierei poligonului. package prob1; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Problema1 extends JPanel{ private int n; private Desen canvas; private JTextField nr,coord,xi,yi,out; private int[] x,y; private int contor=0; private JButton adauga; private AscultatorButon ab; private boolean completat=false; public Problema1(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); nr=new JTextField(2); xi=new JTextField(3); yi=new JTextField(3); adauga=new JButton("Adauga"); adauga.addActionListener(ab); out=new JTextField(10); out.setEditable(false); canvas=new Desen(); JPanel p1=new JPanel(new GridLayout(4,1)); JPanel p2=new JPanel(); JPanel p3=new JPanel(); p2.add(new JLabel("Nr. de puncte:")); p2.add(nr); p1.add(p2); p2=new JPanel();
Transcript
Page 1: Laborator geometrie computationala

1

Laborator 1

Se da un poligon simplu (nu are autointersectii). Pentru un punct M dat se cere sa se determine daca M apartine interiorului poligonului sau M apartine exteriorului poligonului sau M apartine frontierei poligonului. package prob1; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Problema1 extends JPanel{ private int n; private Desen canvas; private JTextField nr,coord,xi,yi,out; private int[] x,y; private int contor=0; private JButton adauga; private AscultatorButon ab; private boolean completat=false; public Problema1(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); nr=new JTextField(2); xi=new JTextField(3); yi=new JTextField(3); adauga=new JButton("Adauga"); adauga.addActionListener(ab); out=new JTextField(10); out.setEditable(false); canvas=new Desen(); JPanel p1=new JPanel(new GridLayout(4,1)); JPanel p2=new JPanel(); JPanel p3=new JPanel(); p2.add(new JLabel("Nr. de puncte:")); p2.add(nr); p1.add(p2); p2=new JPanel();

Page 2: Laborator geometrie computationala

2

p2.add(new JLabel("Coord:")); p2.add(xi); p2.add(yi); p1.add(p2); p2=new JPanel(); p2.add(adauga); p1.add(p2); p2=new JPanel(); p2.add(new JLabel("Output:")); p2.add(out); p1.add(p2); p3.add(p1); this.add(p3,BorderLayout.WEST); this.add(canvas,BorderLayout.CENTER); }//Problema1() --> constructorul "clasei" public class AscultatorButon implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==adauga){ if(!completat){ try{ n=Integer.parseInt(nr.getText()); if(n<3) throw new NumberFormatException(); completat=true; }catch(NumberFormatException nfe) {JOptionPane.showMessageDialog(null,"Dati un numar de puncte valid!","Mesaj de eroare",JOptionPane.WARNING_MESSAGE);} if(n>2){ x=new int[n]; y=new int[n]; Problema1.this.canvas.setN(n); nr.setEnabled(false); } } if(completat){ x[contor]=citesteX(); y[contor]=citesteY(); if(x[contor]==-Integer.MAX_VALUE){xi.setText(""); return;} if(y[contor]==-Integer.MAX_VALUE){yi.setText(""); return;} xi.setText(""); yi.setText("");

Page 3: Laborator geometrie computationala

3

//Problema1.this.canvas.deseneazaPunct(x,y,contor);//v1.0b Problema1.this.canvas.traseazaLinie(x,y,contor); contor++; System.out.println(contor); if(contor==n){ Problema1.this.adauga.setEnabled(false); Problema1.this.canvas.setOutput(out); Problema1.this.canvas.inchide(); } } } }//actionPerformed() private int citesteX(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(xi.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei X !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteX() private int citesteY(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(yi.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei Y !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); }

Page 4: Laborator geometrie computationala

4

return n; }//citesteY() }//AscultatorButon --> clasa interna } Suprafata de desen pentru problema 1 package prob1; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Desen extends Canvas{ private boolean inchis=false,deschis=false,mouse=false; private int[] x,y; private Color[] culoare; private int c=-1,mouseX,mouseY,cc,n=0,nn; private double Mx; private double My; private JTextField output; public Desen(){ setSize(500,500); x=new int[0]; y=new int[0]; } public void paint(Graphics g){ g.setColor(Color.BLACK); g.drawLine(0,0,500,0); g.drawLine(0,0,0,500); g.drawLine(500,0,500,500); g.drawLine(0,500,500,500); //trasez axa x' g.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g.drawString("x",calcX(21),calcY(-1)); g.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y' g.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20));

Page 5: Laborator geometrie computationala

5

g.drawString("y",calcX(-1),calcY(21)); g.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); for(int i=0;i<c;i++){ g.setColor(Color.blue); g.fillOval(calcX(x[i])-2,calcY(y[i])-2,5,4); } for(int i=1;i<c;i++){ g.setColor(culoare[i-1]); g.drawLine(calcX(x[i-1]),calcY(y[i-1]),calcX(x[i]),calcY(y[i])); culoare[i-1]=Color.blue; } if(inchis){ g.setColor(culoare[nn-1]); g.drawLine(calcX(x[0]),calcY(y[0]),calcX(x[c-1]),calcY(y[c-1])); culoare[nn-1]=Color.blue; } g.setColor(Color.red); if(inchis&&mouse) g.drawLine(mouseX, mouseY, calcX(20),mouseY); g.setColor(Color.blue); if ((mouse)&&(inchis)&&(deschis)){ g.setColor(Color.red); //mouse = false; //v1.0b //mouseX=calcX(mouseX);//v1.0b //mouseY=calcY(mouseY);//v1.0b Mx=(mouseX-250)/10.0; My=(250-mouseY)/10.0; System.out.println("paint "+mouseX+" "+mouseY); g.drawLine(mouseX, mouseY, calcX(20),mouseY); boolean ok=algoritm(); if(ok){ output.setText("Frontiera"); for(int i=0;i<nn-1;++i) culoare[i]=Color.blue; } else{ if((cc&1)==1) output.setText("Interior"); else output.setText("Exterior"); }

Page 6: Laborator geometrie computationala

6

deschis=false; repaint(); } }//paint() public void traseazaLinie(int[] x1,int[] y1,int contor){ c=contor+1; x=calcX(x1); y=calcY(y1); repaint(); }//traseazaLinie() public void inchide(){ inchis=true; repaint(); }//inchide() public int[] calcX(int[] xx){ x=new int[c]; for(int i=0;i<c;i++) x[i]=xx[i]; return x; }//calcX(int[] x) public int[] calcY(int[] yy){ y=new int[c]; for(int i=0;i<c;i++) y[i]=yy[i]; return y; }//calcY(int[] y) public int calcX(int x){ return 250+x*10; }//calcX(int x) public int calcY(int y){ return 250-y*10; }//calcY(int y) public boolean algoritm(){ System.out.println("Am ajuns "+Mx+" "+My); cc=0; int xb,xc,yb,yc; System.out.println("Afisare vectori"); for(int i=0;i<c;i++){ System.out.println(x[i]+" "+y[i]);

Page 7: Laborator geometrie computationala

7

} for(int i=1;i<c;i++){ if(y[i-1]<y[i]){ xb=x[i]; yb=y[i]; xc=x[i-1]; yc=y[i-1]; } else{ xb=x[i-1]; yb=y[i-1]; xc=x[i]; yc=y[i]; } if((yb>My)&&(yc<My)) if ((xb*My+Mx*yc+xc*yb-My*xc-yc*xb-yb*Mx)>0) { cc++; System.out.println(cc+" cc de la prima "+xb+" "+yb+" "+xc+" "+yc);culoare[i-1]=Color.cyan;continue;} if(((xb*My+Mx*yc+xc*yb-My*xc-yc*xb-yb*Mx))==0) {System.out.println("Frontiera");return true;} if((yc==My)&&(xc>Mx)) { cc++; culoare[i-1]=Color.cyan;continue;} } System.out.println(x[0]+" "+y[0]+" "+x[nn-1]+" "+y[nn-1]); if(y[0]<y[nn-1]){ xb=x[nn-1]; yb=y[nn-1]; xc=x[0]; yc=y[0]; } else{ xb=x[0]; yb=y[0]; xc=x[nn-1]; yc=y[nn-1]; } if((yb>My)&&(yc<My)) if ((xb*My+Mx*yc+xc*yb-My*xc-yc*xb-yb*Mx)>0) { cc++; System.out.println(cc+" cc de la doi "+xb+" "+yb+" "+xc+" "+yc);culoare[nn-1]=Color.cyan;} if((xb*My+Mx*yc+xc*yb-My*xc-yc*xb-yb*Mx)==0) {System.out.println("Frontiera");deschis=true;return true;} if((yc==My)&&(xc>Mx)) { cc++; culoare[nn-1]=Color.cyan;}

Page 8: Laborator geometrie computationala

8

System.out.println(cc); deschis=true; return false; }//algoritm() public boolean mouseDown(Event e, int x, int y ) { mouseX=x; mouseY=y; if(inchis) mouse = true; deschis=true; repaint(); return true; }//mouseDown() public void setN(int n){ nn=n; culoare=new Color[n+1]; }//setN() public void setOutput(JTextField out){ output=out; }//setOutput }

Page 9: Laborator geometrie computationala

1

Laborator 2

Se dau N puncte in plan, care formeaza un poligon simplu convex. Se cere localizarea a M puncte in plan (eficient). package prob2; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Problema2 extends JPanel{ private int n; private Desen2 canvas; private JTextField nr,coord,xi,yi,out; private int[] x,y; private int contor=0; private JButton adauga; private AscultatorButon ab; private boolean completat=false; public Problema2(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); nr=new JTextField(2); xi=new JTextField(3); yi=new JTextField(3); adauga=new JButton("Adauga"); adauga.addActionListener(ab); out=new JTextField(10); out.setEditable(false); canvas=new Desen2(); JPanel p1=new JPanel(new GridLayout(4,1)); JPanel p2=new JPanel(); JPanel p3=new JPanel(); p2.add(new JLabel("Nr. de puncte:")); p2.add(nr); p1.add(p2); p2=new JPanel(); p2.add(new JLabel("Coord:")); p2.add(xi); p2.add(yi); p1.add(p2);

Page 10: Laborator geometrie computationala

2

p2=new JPanel(); p2.add(adauga); p1.add(p2); p2=new JPanel(); p2.add(new JLabel("Output:")); p2.add(out); p1.add(p2); p3.add(p1); this.add(p3,BorderLayout.WEST); this.add(canvas,BorderLayout.CENTER); }//Problema2() --> constructorul "clasei" public class AscultatorButon implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==adauga){ if(!completat){ try{ n=Integer.parseInt(nr.getText()); if(n<4) throw new NumberFormatException(); completat=true; }catch(NumberFormatException nfe) {JOptionPane.showMessageDialog(null,"Dati un numar de puncte valid!","Mesaj de eroare",JOptionPane.WARNING_MESSAGE);} if(n>3){ x=new int[n]; y=new int[n]; //Problema2.this.canvas.setN(n); nr.setEnabled(false); } } if(completat){ x[contor]=citesteX(); y[contor]=citesteY(); //if(x[contor]==-Integer.MAX_VALUE){xi.setText(""); return;} //if(y[contor]==-Integer.MAX_VALUE){yi.setText(""); return;} xi.setText(""); yi.setText(""); //Problema1.this.canvas.deseneazaPunct(x,y,contor);//v1.0b //Problema1.this.canvas.traseazaLinie(x,y,contor); contor++;

Page 11: Laborator geometrie computationala

3

//System.out.println(contor); if(contor==n){ Algoritm2 a=new Algoritm2((x[0]+x[1]+x[2])/3.0,(y[0]+y[1]+y[2])/3.0); for(int i=0;i<x.length;++i) a.adaugaPunct(x[i],y[i]); Problema2.this.canvas.setAlgoritm(a); Problema2.this.adauga.setEnabled(false); Problema2.this.canvas.setOutput(out); } } } }//actionPerformed() private int citesteX(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(xi.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei X !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteX() private int citesteY(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(yi.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false;

Page 12: Laborator geometrie computationala

4

JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei Y !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteY() } } Algoritmul pt. problema 2 package prob2; import java.util.*; public class Algoritm2{ private Set lista; private double gx, gy; private Punct cautP; private Punct[] array; private int[] punct; private int it; public Algoritm2(double gx, double gy){ lista=new TreeSet(); this.gx=gx; this.gy=gy; }//constructor public void adaugaPunct(int x, int y){ Punct p=new Punct(x,y,gx, gy); lista.add(p); } public void afiseaza(){ Iterator i=lista.iterator(); while(i.hasNext()){ Punct p1=(Punct)(i.next()); System.out.println(p1.getX()+" "+p1.getY()); } } public Punct[] cautaBinar(double x, double y){ cautP=new Punct(x, y, gx, gy); int n=lista.size(); it=0; punct=new int[n]; array=new Punct[n+2];

Page 13: Laborator geometrie computationala

5

Iterator i=lista.iterator(); int k=1; while(i.hasNext()) array[k++]=(Punct)(i.next()); array[n+1]=array[1]; array[0]=array[n]; cautareBinara(0,n+1); Punct[] rez=new Punct[2]; if(it<1) System.out.println("err Algoritm"); int l=0; rez[0]=array[punct[it]]; if((rez[0].getX()*cautP.getY()+cautP.getX()*gy+gx*rez[0].getY()-gx*cautP.getY()-rez[0].getX()*gy-cautP.getX()*rez[0].getY())>0) { int lll=punct[it]; while(l!=1){ if(punct[--it]>lll) {rez[1]=array[punct[it]];l++;} } } else { int lll=punct[it]; while(l!=1){ if(punct[--it]<lll) {rez[1]=array[punct[it]];l++;} } Punct paux=rez[0]; rez[0]=rez[1]; rez[1]=paux; } return rez; } public void cautareBinara(int i, int j){ /*if (i==j) System.out.println(i); else */if (i<j) { int nr=(i+j)/2; int ll=nr; //if(nr==array.length-1) ll=1; punct[it++]=ll; Punct p1=cautP; Punct p2=array[nr]; System.out.println("Sunt aici "+nr); boolean ok=false; if(p1.getCadran()<p2.getCadran()) {ok=true;System.out.println("Sunt aici1 "+nr);} if(p1.getCadran()==p2.getCadran()){

Page 14: Laborator geometrie computationala

6

if((p1.getX()*gy+gx*p2.getY()+p2.getX()*p1.getY()-gy*p2.getX()-p2.getY()*p1.getX()-p1.getY()*gx)<0) ok=true;} if(ok) cautareBinara(i,nr-1); else cautareBinara(nr+1,j); } else {System.out.println((i+j)/2); int nr=(i+j)/2; int ll=nr; //if(nr==array.length-1) ll=1; punct[it]=ll;} } public int[] getX(){ int nr=lista.size(); Iterator i=lista.iterator(); int[] xv=new int[nr]; int k=0; while(i.hasNext()) xv[k++]=(int)(((Punct)(i.next())).getX()); return xv; } public int[] getY(){ int nr=lista.size(); Iterator i=lista.iterator(); int[] yv=new int[nr]; int k=0; while(i.hasNext()) yv[k++]=(int)(((Punct)(i.next())).getY()); return yv; } public double getGX(){ return gx; } public double getGY(){ return gy; } } Suprafata de desen pentru problema 2 package prob2; import javax.swing.*; import java.awt.*; import java.awt.event.*;

Page 15: Laborator geometrie computationala

7

public class Desen2 extends Canvas{ private boolean mouseStart=false ,mouseSelect=false, solutie=false, poli=false; private int[] x,y; private Color[] culoare; private int mouseX,mouseY,cc,n=0,nn; private double Mx; private double My; private JTextField output; private Algoritm2 a; private Punct[] punct; private int centrux,centruy; public Desen2(){ setSize(500,500); } public void paint(Graphics g){ g.setColor(Color.BLACK); g.drawLine(0,0,500,0); g.drawLine(0,0,0,500); g.drawLine(500,0,500,500); g.drawLine(0,500,500,500); //trasez axa x' g.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g.drawString("x",calcX(21),calcY(-1)); g.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y' g.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20)); g.drawString("y",calcX(-1),calcY(21)); g.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); if(poli){ for(int i=0;i<x.length;++i) System.out.println(x[i]+" "+y[i]); for(int i=1;i<x.length;++i) g.drawLine(calcX(x[i-1]),calcY(y[i-1]),calcX(x[i]), calcY(y[i])); g.drawLine(calcX(x[0]),calcY(y[0]),calcX(x[x.length-1]),calcY(y[x.length-1]));

Page 16: Laborator geometrie computationala

8

centrux=250+(int)((a.getGX())*10); centruy=250-(int)((a.getGY())*10); System.out.println(centrux+" centru "+centruy); for(int i=0;i<x.length;++i) g.drawLine(centrux,centruy,calcX(x[i]),calcY(y[i])); g.fillOval(centrux-2,centruy-2,5,4); } if(mouseSelect){ g.fillOval(mouseX-2,mouseY-2,5,4); punct=a.cautaBinar(Mx,My); mouseStart=true; solutie=true;; } if(solutie){ g.setColor(Color.blue); g.drawLine(calcX((int)(punct[0].getX())),calcY((int)(punct[0].getY())),calcX((int)(punct[1].getX())),calcY((int)(punct[1].getY()))); g.drawLine(centrux, centruy, calcX((int)(punct[0].getX())),calcY((int)(punct[0].getY()))); g.drawLine(centrux, centruy,calcX((int)(punct[1].getX())),calcY((int)(punct[1].getY()))); double det=punct[1].getX()*My+Mx*punct[0].getY()+punct[0].getX()*punct[1].getY()-My*punct[0].getX()-punct[0].getY()*punct[1].getX()-punct[1].getY()*Mx; System.out.println(det); if(det>0) output.setText("Interior"); if(det<0) output.setText("Exterior"); if(det==0) output.setText("Frontiera"); } }//paint() public int calcX(int x){ return 250+x*10; }//calcX(int x) public int calcY(int y){ return 250-y*10; }//calcY(int y)

Page 17: Laborator geometrie computationala

9

public boolean mouseDown(Event e, int x, int y ) { if(mouseStart){ mouseX=x; mouseY=y; Mx=(mouseX-250)/10.0; My=(250-mouseY)/10.0; mouseSelect=true; mouseStart=false; repaint(); } return true; }//mouseDown() public void setOutput(JTextField out){ output=out; }//setOutput public void setAlgoritm(Algoritm2 a){ this.a=a; mouseStart=true; x=a.getX(); y=a.getY(); poli=true; a.afiseaza(); repaint(); } } Clasa ajutatoare, Creaza obiecte de tip Punct package prob2; public class Punct implements Comparable{ private double x; private double y; private int cadran; private double gx, gy; public Punct(int x, int y, double gx, double gy){ this.x=x; this.y=y; this.gx=gx; this.gy=gy; calculeazaCadran(); System.out.println(cadran==0?"err cadran":"cadran "+cadran); }

Page 18: Laborator geometrie computationala

10

public Punct(double x, double y, double gx, double gy){ this.x=x; this.y=y; this.gx=gx; this.gy=gy; calculeazaCadran(); System.out.println(cadran==0?"err cadran":"cadran "+cadran); } public int compareTo(Object o){ Punct p=(Punct)o; if(cadran<p.getCadran()) return -10; if(cadran>p.getCadran()) return +10; if((x*gy+gx*p.getY()+p.getX()*y-gy*p.getX()-p.getY()*x-y*gx)<0) return -10; else return +10; } public void calculeazaCadran(){ if((x>gx)&&(y>=gy)) cadran=1; if((x<=gx)&&(y>gy)) cadran=2; if((x<gx)&&(y<=gy)) cadran=3; if((x>=gx)&&(y<gy)) cadran=4; } public int getCadran(){ return cadran; } public double getX(){ return x; } public double getY(){ return y; } }

Page 19: Laborator geometrie computationala

1

Laborator 3

Se da un PSLG printr-un DCEL. Se cere sa se localizeze un punct (sau mai multe optim) in acest PSLG. package prob3; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Problema3 extends JPanel{ private int n; private Desen3 canvas; private JTextField nr,coord,xi,yi,out,xj,yj,f1,f2,pp1,pp2; private int[] x,y,x1,y1,vf1,vf2,vp1,vp2; private int contor=0; private JButton adauga; private AscultatorButon ab; private boolean completat=false; private Muchie[] muchie; private Punct3[] p; private Set puncte; public Problema3(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); canvas=new Desen3(); nr=new JTextField(2); xi=new JTextField(3); yi=new JTextField(3); xj=new JTextField(3); yj=new JTextField(3); f1=new JTextField(3); f2=new JTextField(3); pp1=new JTextField(3); pp2=new JTextField(3); adauga=new JButton("Adauga"); adauga.addActionListener(ab); out=new JTextField(10); out.setEditable(false); JPanel p1=new JPanel(new GridLayout(6,1)); JPanel p2=new JPanel(); JPanel p3=new JPanel(); p2.add(new JLabel("Nr. de muchii:"));

Page 20: Laborator geometrie computationala

2

p2.add(nr); p1.add(p2); p2=new JPanel(); p2.add(new JLabel("V1:")); p2.add(xi); p2.add(yi); p1.add(p2); p2=new JPanel(); p2.add(new JLabel("V2:")); p2.add(xj); p2.add(yj); p1.add(p2); p2=new JPanel(); p2.add(new JLabel("F:")); p2.add(f1); p2.add(f2); p1.add(p2); p2=new JPanel(); p2.add(new JLabel("P:")); p2.add(pp1); p2.add(pp2); p1.add(p2); p2=new JPanel(); p2.add(adauga); p1.add(p2); /*** p2=new JPanel(); p2.add(new JLabel("Output:")); p2.add(out); p1.add(p2); ***/ p3.add(p1); this.add(p3,BorderLayout.WEST); this.add(canvas,BorderLayout.CENTER); }//Problema3() --> constructorul "clasei" public class AscultatorButon implements ActionListener{ public void actionPerformed(ActionEvent e){

Page 21: Laborator geometrie computationala

3

if(e.getSource()==adauga){ if(!completat){ try{ n=Integer.parseInt(nr.getText()); if(n!=0) throw new NumberFormatException(); completat=true; }catch(NumberFormatException nfe) {JOptionPane.showMessageDialog(null,"Dati un numar de puncte valid!","Mesaj de eroare",JOptionPane.WARNING_MESSAGE);} if(n==0){ x=new int[n]; y=new int[n]; x1=new int[n]; y1=new int[n]; vp1=new int[n]; vp2=new int[n]; vf1=new int[n]; vf2=new int[n]; muchie=new Muchie[18]; puncte=new TreeSet(); nr.setEnabled(false); //Muchie[] muchie=new Muchie[18]; p=new Punct3[12]; p[0]=new Punct3(5,-10); p[1]=new Punct3(-1,-9); p[2]=new Punct3(7,-6); p[3]=new Punct3(3,-5); p[4]=new Punct3(-10,-4); p[5]=new Punct3(-8,1); p[6]=new Punct3(2,3); p[7]=new Punct3(-15,5); p[8]=new Punct3(1,7); p[9]=new Punct3(-3,8); p[10]=new Punct3(8,10); p[11]=new Punct3(1,12); //Punct3[12]=new Punct3(,); //Punct3[13]=new Punct3(,); muchie[0]=new Muchie( 1, new Punct3(-15,5), new Punct3(-3,8), 0, 1,

Page 22: Laborator geometrie computationala

4

3, 13 ) ; muchie[1]=new Muchie( 2, new Punct3(1,7), new Punct3(8,10), 2, 5, 16, 15 ) ; muchie[2]=new Muchie( 3, new Punct3(-10,-4), new Punct3(-15,5), 0, 1, 4, 1 ) ; muchie[3]=new Muchie( 4, new Punct3(-1,-9), new Punct3(-10,-4), 0, 4, 12, 17 ) ; muchie[4]=new Muchie( 5, new Punct3(-1,-9), new Punct3(3,-5), 4, 7, 4, 14 ) ; muchie[5]=new Muchie(

Page 23: Laborator geometrie computationala

5

6, new Punct3(2,3), new Punct3(-3,8), 3, 2, 8, 7 ) ; muchie[6]=new Muchie( 7, new Punct3(-3,8), new Punct3(1,12), 0, 2, 1, 18 ) ; muchie[7]=new Muchie( 8, new Punct3(5,-10), new Punct3(2,3), 3, 6, 14, 9 ) ; muchie[8]=new Muchie( 9, new Punct3(7,-6), new Punct3(2,3), 6, 5, 10, 16 ) ; muchie[9]=new Muchie( 10, new Punct3(5,-10), new Punct3(7,-6), 6, 0, 8,

Page 24: Laborator geometrie computationala

6

15 ) ; muchie[10]=new Muchie( 11, new Punct3(3,-5), new Punct3(-8,1), 4, 3, 5, 13 ) ; muchie[11]=new Muchie( 12, new Punct3(5,-10), new Punct3(-1,-9), 0, 7, 10, 5 ) ; muchie[12]=new Muchie( 13, new Punct3(-8,1), new Punct3(-3,8), 1, 3, 14, 11 ) ; muchie[13]=new Muchie( 14, new Punct3(5,-10), new Punct3(3,-5), 7, 3, 12, 5 ) ; muchie[14]=new Muchie( 15, new Punct3(7,-6),

Page 25: Laborator geometrie computationala

7

new Punct3(8,10), 5, 0, 9, 18 ) ; muchie[15]=new Muchie( 16, new Punct3(2,3), new Punct3(1,7), 2, 5, 6, 2 ) ; muchie[16]=new Muchie( 17, new Punct3(-10,-4), new Punct3(-8,1), 1, 4, 3, 11 ) ; muchie[17]=new Muchie( 18, new Punct3(8,10), new Punct3(1,12), 2, 0, 2, 7 ) ; } } /*if(completat){ x[contor]=citesteX(); y[contor]=citesteY(); x1[contor]=citesteX1(); y1[contor]=citesteY1(); vf1[contor]=citesteF1();

Page 26: Laborator geometrie computationala

8

vf2[contor]=citesteF2(); vp1[contor]=citesteP1(); vp2[contor]=citesteP2(); puncte.add(new Punct3(x[contor],y[contor])); puncte.add(new Punct3(x1[contor],y1[contor])); muchie[contor]=new Muchie(contor,new Punct3(x[contor],y[contor]),new Punct3(x1[contor],y1[contor]),vf1[contor], vf2[contor],vp1[contor],vp2[contor]); //if(x[contor]==-Integer.MAX_VALUE){xi.setText(""); return;} //if(y[contor]==-Integer.MAX_VALUE){yi.setText(""); return;} xi.setText(""); yi.setText(""); xj.setText(""); yj.setText(""); f1.setText(""); f2.setText(""); pp1.setText(""); pp2.setText(""); //Problema1.this.canvas.deseneazaPunct(x,y,contor);//v1.0b //Problema1.this.canvas.traseazaLinie(x,y,contor); contor++; //System.out.println(contor); */ if(contor==n){ ArrayList al=new ArrayList(puncte); Object[] ob=al.toArray(); Punct3[] punct=new Punct3[ob.length]; for(int k=0;k<ob.length;++k) punct[k]=(Punct3)ob[k]; Algoritm3 a=new Algoritm3(p, muchie); Problema3.this.canvas.setAlgoritm(a); Problema3.this.adauga.setEnabled(false); Problema3.this.canvas.setOutput(out); //} } } }//actionPerformed() private int citesteX(){

Page 27: Laborator geometrie computationala

9

boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(xi.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei X !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteX() private int citesteY(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(yi.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei Y !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteY() private int citesteX1(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(xj.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei X !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE);

Page 28: Laborator geometrie computationala

10

} return n; }//citesteX1() private int citesteY1(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(yj.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei Y !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteY1() private int citesteF1(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(f1.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei Y !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteF1() private int citesteF2(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(f2.getText()); if(n<-20) throw new NumberFormatException();

Page 29: Laborator geometrie computationala

11

if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei Y !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteF2() private int citesteP1(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(pp1.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei Y !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteP1() private int citesteP2(){ boolean ok=true; int n=-Integer.MAX_VALUE; while(ok) try{ ok=false; n=Integer.parseInt(pp2.getText()); if(n<-20) throw new NumberFormatException(); if(n>+20) throw new NumberFormatException(); }catch(NumberFormatException nfe){ ok=false; JOptionPane.showMessageDialog(null,"Dati un numar valid pt. coordonata axei Y !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } return n; }//citesteP2() } }

Page 30: Laborator geometrie computationala

12

Algoritmul pt. problema 3 package prob3; import java.util.*; public class Algoritm3{ private Punct3[] a; private Muchie[] dcel; private List matura; //maturatorul private List[] Lm; //lista de stari de slavare a starii maturei private Object[] lespede; private double x,y; private Muchie mBin; public Algoritm3(Punct3[] a,Muchie[] dcel){ this.a=a; this.dcel=dcel; Arrays.sort(a); interschimba(); listeaza(); matura(); } /*** private void sorteaza(){ for(int i=0;i<a.length-1;++i) for(int j=i+1;j<a.length;++j){ if(a[i].getY()>a[j].getY()){ Punct3 aux; aux=a[i]; a[i]=a[j]; a[j]=aux; } } } ***/ private void interschimba(){ for(int i=0;i<dcel.length;++i){ if(((dcel[i].getV1()).compareTo(dcel[i].getV2()))>0) dcel[i].inverseaza(); } } public void listeaza(){ for(int i=0;i<a.length;++i){

Page 31: Laborator geometrie computationala

13

TreeSet c=new TreeSet(); TreeSet b=new TreeSet(); for(int j=0;j<dcel.length;++j){ if(((dcel[j].getV1()).compareTo(a[i]))==0) c.add(dcel[j]); if(((dcel[j].getV2()).compareTo(a[i]))==0) b.add(dcel[j]); } Iterator it=c.iterator(); System.out.print("+"+(i+1)+"\t:"); while(it.hasNext()) System.out.print(" "+((Muchie)(it.next())).getNr()); System.out.println(); a[i].setC(c); it=b.iterator(); System.out.print("-"+(i+1)+"\t:"); while(it.hasNext()) System.out.print(" "+((Muchie)(it.next())).getNr()); System.out.println(); a[i].setB(b); } } public void matura(){ Lm=new ArrayList[a.length-1]; matura=new ArrayList(a[0].getC()); Lm[0]=new ArrayList(matura); for(int i=1;i<a.length-1;++i){ Iterator itb=(a[i].getB()).iterator(); while(itb.hasNext()){ Muchie mb=(Muchie)(itb.next()); if(matura.contains(mb)){ int poz=matura.indexOf(mb); matura.remove(poz); Iterator itc=(a[i].getC()).iterator(); while(itc.hasNext()){ Muchie mc=(Muchie)(itc.next()); if(!matura.contains(mc)){ matura.add(poz++,mc); } } } }

Page 32: Laborator geometrie computationala

14

Lm[i]=new ArrayList(matura); } System.out.println("---***---...afisari stari matura...---***---"); for(int i=0;i<Lm.length;++i){ Object[] vAux=Lm[i].toArray(); System.out.print(i+"\t- "+vAux.length+"- :\t"); for(int j=0;j<vAux.length;++j){ System.out.print(((Muchie)(vAux[j])).getNr()+" "); } System.out.println(); } } public int cauta(double x, double y){ this.x=x; this.y=y; System.out.println(cautaLespede(x,y)); int rez=cautaLespede(x,y)-1; if(rez>a.length-2) rez=a.length-2; if(rez<0) return 0; lespede=Lm[rez].toArray(); cautaBinar(0,lespede.length-1); System.out.println(mBin.getNr()); int F=0; if(calcDet (mBin.getV1().getX(), mBin.getV1().getY(), x, y, mBin.getV2().getX(), mBin.getV2().getY() )>0) F=mBin.getF2(); else F=mBin.getF1(); return F; } public void cautaBinar(int s, int d){ int mijloc=(s+d)/2; if(s<d){ if(calcDet (((Muchie)(lespede[mijloc])).getV1().getX(), ((Muchie)(lespede[mijloc])).getV1().getY(), x, y, ((Muchie)(lespede[mijloc])).getV2().getX(), ((Muchie)(lespede[mijloc])).getV2().getY()) >0

Page 33: Laborator geometrie computationala

15

) cautaBinar(mijloc+1,d); else cautaBinar(s,mijloc-1); } else{ mBin=((Muchie)(lespede[mijloc])); } } public int cautaLespede(double x, double y){ return ~Arrays.binarySearch(a,new Punct3(x,y)); } //intoarce in a cata lespede se afla punctul cautat public double calcDet(double x1,double y1, double x2, double y2, double x3, double y3){ return x1*y2+x2*y3+x3*y1-y2*x3-y3*x1-y1*x2; } public Muchie[] getDCEL(){ return dcel; } } Suprafata de desen pentru problema 3 package prob3; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Graphics.*; import java.awt.Graphics2D.*; import java.awt.geom.*; class Desen3 extends Canvas{ private boolean drawPoli=false, mouseStart=false,mouseSelect=false; private int[] x,y; private Color[] culoare; private int mouseX,mouseY,cc,n=0,nn,MMx,MMy; private double Mx,MMMx; private double My,MMMy; private JTextField output; private Algoritm3 a; //private Punct[] punct; private int centrux,centruy,F;

Page 34: Laborator geometrie computationala

16

public Desen3(){ setSize(500,500); } public void paint(Graphics g){ Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.BLACK); g2.drawLine(0,0,500,0); g2.drawLine(0,0,0,500); g2.drawLine(500,0,500,500); g2.drawLine(0,500,500,500); //trasez axa x' g2.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g2.drawString("x",calcX(21),calcY(-1)); g2.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g2.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y' g2.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20)); g2.drawString("y",calcX(-1),calcY(21)); g2.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g2.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); //desenze poligonul if(drawPoli){ Muchie[] dcel=a.getDCEL(); for(int i=0;i<dcel.length;++i){ g2.drawLine(calcX((int)dcel[i].getV1().getX()), calcY((int)dcel[i].getV1().getY()), calcX((int)dcel[i].getV2().getX()), calcY((int)dcel[i].getV2().getY()) ); int mijlocX=(calcX((int)dcel[i].getV1().getX())+calcX((int)dcel[i].getV2().getX()))/2; int mijlocY=(calcY((int)dcel[i].getV1().getY())+calcY((int)dcel[i].getV2().getY()))/2; g2.fillOval(calcX((int)dcel[i].getV1().getX()),calcY((int)dcel[i].getV1().getY()),3,3);

Page 35: Laborator geometrie computationala

17

g2.fillOval(calcX((int)dcel[i].getV2().getX()),calcY((int)dcel[i].getV2().getY()),3,3); //desenatul arcelor corect!!!!! double mX=(dcel[i].getV1().getX()+dcel[i].getV2().getX())/2.0; double mY=(dcel[i].getV1().getY()+dcel[i].getV2().getY())/2.0; double mmX=mX; double mmY=mY; double mp1X=mX-0.3; double mp1Y=mY-0.3; double mp2X=mX+0.3; double mp2Y=mY-0.3; double inclinare=0d; if(dcel[i].getV1().getX()<dcel[i].getV2().getX()) inclinare=-Math.PI/2; else inclinare=Math.PI/2; g2.setColor(Color.red); //double teta=(Math.sqrt(Math.pow(dcel[i].getV1().getX()-mX,2)+Math.pow(dcel[i].getV1().getY()-mY,2)))/(Math.sqrt(Math.pow(dcel[i].getV1().getY()-mY,2))); double cosu=Math.sqrt(Math.cos(Math.pow(dcel[i].getV1().getY()-mY,2)))/Math.sqrt((Math.pow(dcel[i].getV1().getX()-mX,2))); double tangenta=(dcel[i].getV2().getY()-dcel[i].getV1().getY())/(dcel[i].getV2().getX()-dcel[i].getV1().getX()); double teta=Math.atan(tangenta)+inclinare; AffineTransform at = new AffineTransform(); mX-=mp1X; mY-=mp1Y; at.rotate(teta,mX,mY); double newX = at.getTranslateX(); double newY = at.getTranslateY(); newX+=mp1X; newY+=mp1Y;

Page 36: Laborator geometrie computationala

18

g2.drawLine(mijlocX,mijlocY,calcX(newX),calcY(newY)); AffineTransform at1 = new AffineTransform(); mmX-=mp2X; mmY-=mp2Y; at1.rotate(teta,mmX,mmY); newX = at1.getTranslateX(); newY = at1.getTranslateY(); newX+=mp2X; newY+=mp2Y; g2.drawLine(mijlocX,mijlocY,calcX(newX),calcY(newY)); // g2.drawLine(mijlocX,mijlocY,mijlocX+3,mijlocY+3); // g2.drawLine(mijlocX,mijlocY,mijlocX-3,mijlocY+3); g2.drawString(""+dcel[i].getNr(),mijlocX+2,mijlocY+2); g2.setColor(Color.black); } } if(mouseSelect){ g.setColor(Color.blue); g.fillOval(mouseX,mouseY,1,1); g.drawLine(mouseX+2,mouseY-2,mouseX-2,mouseY+2); g.drawLine(mouseX-2,mouseY-2,mouseX+2,mouseY+2); Muchie[] dcel=a.getDCEL(); for(int i=0;i<dcel.length;++i){ if((dcel[i].getF1()==F)||(dcel[i].getF2()==F)) g.drawLine(calcX((int)dcel[i].getV1().getX()), calcY((int)dcel[i].getV1().getY()), calcX((int)dcel[i].getV2().getX()), calcY((int)dcel[i].getV2().getY()) ); } } g.drawString("( "+MMMx+","+MMMy+" )",MMx,MMy);

Page 37: Laborator geometrie computationala

19

//centrux=250+(int)((a.getGX())*10); // centruy=250-(int)((a.getGY())*10); }//paint() public int calcX(int x){ return 250+x*10; }//calcX(int x) public int calcY(int y){ return 250-y*10; }//calcY(int y) public boolean mouseDown(Event e, int x, int y ) { if(mouseStart){ mouseX=x; mouseY=y; Mx=(mouseX-250)/10.0; My=(250-mouseY)/10.0; mouseSelect=true; //mouseStart=false; F=a.cauta(Mx,My); repaint(); } return true; }//mouseDown() public boolean mouseMove(Event e, int x, int y){ if(mouseStart){ MMx=x; MMy=y; MMMx=(MMx-250)/10.0; MMMy=(250-MMy)/10.0; } repaint(); return true; } public void setOutput(JTextField out){ output=out; }//setOutput public void setAlgoritm(Algoritm3 a){

Page 38: Laborator geometrie computationala

20

this.a=a; drawPoli=true; mouseStart=true; repaint(); } public double calcDet(double x1,double y1, double x2, double y2, double x3, double y3){ return x1*y2+x2*y3+x3*y1-y2*x3-y3*x1-y1*x2; } public int calcX(double x){ return (int)(250+x*10); }//calcX(int x) public int calcY(double y){ return (int)(250-y*10); }//calcY(int y) //public void rotate() } // proiect Claudiu Fercu , Valeria Campeanu si Banciu Raluca Clasa ajutatoare, Creaza obiecte de tip Punct3 package prob3; import java.util.*; public class Punct3 implements Comparable{ private double x,y; private Set b,c; public Punct3(double x,double y){ this.x=x; this.y=y; } public double getX(){ return x; } public double getY(){ return y; }

Page 39: Laborator geometrie computationala

21

public int compareTo(Object o){ if(y>((Punct3)o).getY()) return 255; if((y==((Punct3)o).getY())&&(x==((Punct3)o).getX())) return 0; return -256; } public void setC(TreeSet c){ this.c=c; } public void setB(TreeSet b){ this.b=b; } public Set getC(){ return c; } public Set getB(){ return b; } } Clasa ajutatoare. Creaza obiecte de tip Muchie package prob3; public class Muchie implements Comparable{ private int nr,f1,f2,p1,p2; private Punct3 v1,v2; public Muchie(int nr, Punct3 v1, Punct3 v2, int f1, int f2, int p1, int p2){ this.nr=nr; this.v1=v1; this.v2=v2; this.f1=f1; this.f2=f2; this.p1=p1; this.p2=p2; } public int getNr(){ return nr; } public Punct3 getV1(){ return v1;

Page 40: Laborator geometrie computationala

22

} public Punct3 getV2(){ return v2; } public int getF1(){ return f1; } public int getF2(){ return f2; } public int getP1(){ return p1; } public int getP2(){ return p2; } public int compareTo(Object o){ Muchie m=(Muchie)o; if (f1==0) return -256; if (f2==0) return +255; if ((v1.equals(m.getV1()))&&(v2.equals(m.getV2()))) return 0; if (f2==m.getF1()) return -256; return 255; } public void inverseaza(){ Punct3 paux; paux=v1; v1=v2; v2=paux; f1=f1+f2; f2=f1-f2; f1=f1-f2; p1=p1+p2; p2=p1-p2; p2=p1-p2; } }

Page 41: Laborator geometrie computationala

1

Laborator 4

Se dau N puncte in plan. Se da un dreptunghi prin 2 puncte A si C. Se cere sa se spuna cate puncte se afla in interiorul dreptunghiului si care sunt. Varianta neoptimizata pentru mai multe dreptunghiuri. package prob4; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Problema4 extends JPanel{ private Desen4 canvas; private int[] x,y; private boolean completat=false; public Problema4(){ //this.setLayout(new BorderLayout()); JPanel p1=new JPanel(); canvas=new Desen4(); //p1.add(canvas); this.add(canvas); }//Problema4() --> constructorul "clasei" } Suprafata de desen pentru problema 4 package prob4; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Desen4 extends Canvas{ private Vector x,y,x1,y1; private boolean mousePrim=false, mouseSecund=false;//prima var se refera la dc ai introdus aleator punctele, a doua daca ai terminat de introdus punctele private double mx,my,min,max; private int contor=0,nr=0;

Page 42: Laborator geometrie computationala

2

private Vector b; public Desen4(){ setSize(501,501); mousePrim=true; x=new Vector(); y=new Vector(); x1=new Vector(); y1=new Vector(); b=new Vector(); System.out.println("Aci"); this.enableEvents(MouseEvent.MOUSE_CLICKED); //b=new boolean[100]; } public void paint(Graphics g){ //trasez chenarul g.setColor(Color.BLACK); g.drawLine(0,0,500,0); g.drawLine(0,0,0,500); g.drawLine(500,0,500,500); g.drawLine(0,500,500,500); //trasez axa x' g.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g.drawString("x",calcX(21),calcY(-1)); g.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y' g.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20)); g.drawString("y",calcX(-1),calcY(21)); g.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); //scriem punctele for(int i=0;i<x.size();i++) g.fillOval(((Integer)(x.elementAt(i))).intValue()-2, ((Integer)(y.elementAt(i))).intValue()-2,4,4); g.setColor(Color.red); //scriem punctele dreptunghilui for(int i=0;i<x1.size();i++) //g.f g.fillOval(((Integer)(x1.elementAt(i))).intValue()-2, ((Integer)(y1.elementAt(i))).intValue()-2,4,4); //trasam dreptele dretunghiului

Page 43: Laborator geometrie computationala

3

if(x1.size()==4){ g.drawLine( ((Integer) (x1.elementAt(0))).intValue(), ((Integer) (y1.elementAt(0))).intValue(), ((Integer) (x1.elementAt(2))).intValue(), ((Integer) (y1.elementAt(2))).intValue()); g.drawLine( ((Integer) (x1.elementAt(1))).intValue(), ((Integer) (y1.elementAt(1))).intValue(), ((Integer) (x1.elementAt(3))).intValue(), ((Integer) (y1.elementAt(3))).intValue()); g.drawLine( ((Integer) (x1.elementAt(0))).intValue(), ((Integer) (y1.elementAt(0))).intValue(), ((Integer) (x1.elementAt(3))).intValue(), ((Integer) (y1.elementAt(3))).intValue()); g.drawLine( ((Integer) (x1.elementAt(2))).intValue(), ((Integer) (y1.elementAt(2))).intValue(), ((Integer) (x1.elementAt(1))).intValue(), ((Integer) (y1.elementAt(1))).intValue()); g.setColor(Color.RED); System.out.println(b.size()); for(int i=0;i<x.size();i++) if (((String)(b.elementAt(i))).equals("true")) g.drawOval(((Integer)(x.elementAt(i))).intValue()-2, ((Integer)(y.elementAt(i))).intValue()-2,4,4); if(nr==1) g.drawString("Este un punct",240,440); else g.drawString("Sunt "+nr+" puncte",240,440); nr=0; contor=0; x1=new Vector(); y1=new Vector(); b=new Vector(); mousePrim=false; } }//paint() public int calcX(int x){ return 250+x*10; }//calcX(int x) public int calcY(int y){ return 250-y*10; }//calcY(int y) /* public boolean mouseDown(Event e,int x,int y){ System.out.println("aci"); return true; }*/ public void processMouseEvent(MouseEvent e) { System.out.println("jkhajs");

Page 44: Laborator geometrie computationala

4

if((mousePrim) && (e.getButton()==MouseEvent.BUTTON1) && (e.getID()==MouseEvent.MOUSE_CLICKED)){ contor++; //if(contor%3==0){ System.out.println("DA"); int mouseX=e.getX(); int mouseY=e.getY(); mx=(mouseX-250)/10.0; my=(250-mouseY)/10.0; this.x.add(mouseX); this.y.add(mouseY); //} } else { if((e.getButton()==MouseEvent.BUTTON3) && (e.getID()==MouseEvent.MOUSE_CLICKED)){ mousePrim=false; System.out.println("apasat mouse 2"); } } if((mousePrim==false) && (e.getButton()==MouseEvent.BUTTON1) && (e.getID()==MouseEvent.MOUSE_CLICKED) ){ contor++; //if(contor%3==0){ int mouseX=e.getX(); int mouseY=e.getY(); mx=(mouseX-250)/10.0; my=(250-mouseY)/10.0; contor=x1.size(); System.out.println(contor); x1.add(mouseX); y1.add(mouseY); if(x1.size()==2){ mx=Math.max(((Integer)(x1.elementAt(0))).intValue(),((Integer)(x1.elementAt(1))).intValue()); my=Math.min(((Integer)(y1.elementAt(0))).intValue(),((Integer)(y1.elementAt(1))).intValue()); x1.add((int)mx); y1.add((int)my); mx=Math.min(((Integer)(x1.elementAt(0))).intValue(),((Integer)(x1.elementAt(1))).intValue());

Page 45: Laborator geometrie computationala

5

my=Math.max(((Integer)(y1.elementAt(0))).intValue(),((Integer)(y1.elementAt(1))).intValue()); x1.add((int)mx); y1.add((int)my); if(((Integer)(x1.elementAt(0))).intValue()> ((Integer)(x1.elementAt(1))).intValue() ) { Object o=x1.elementAt(0); x1.setElementAt(x1.elementAt(1),0); x1.setElementAt(o,1); o=y1.elementAt(0); y1.setElementAt(y1.elementAt(1),0); y1.setElementAt(o,1); //b=new boolean[x.size()]; } algoritm(); } //} } repaint(); //return true; }//mouseDown() public void algoritm(){ System.out.println(x.size()+"x size"); for(int i=0;i<x.size();++i) b.add("false"); System.out.println(x.size()+"b size"); for(int i=0;i<x.size();i++){ if( ((Integer) (x.elementAt(i))).intValue() > ((Integer) (x1.elementAt(0))).intValue() ) if (((Integer) (x.elementAt(i))).intValue() < ((Integer) (x1.elementAt(1))).intValue()) if ( ((Integer) (y.elementAt(i))).intValue() > ((Integer) (y1.elementAt(0))).intValue()) if ( ((Integer) (y.elementAt(i))).intValue() < ((Integer) (y1.elementAt(1))).intValue()) { nr++; b.setElementAt("true",i); } } } }

Page 46: Laborator geometrie computationala

1

Laborator 5

Se da un PSLG printr-un DCEL. Sa se localizeze M puncte in PSLG (optim) prin Metoda Lantului. package prob5; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class Problema5 extends JPanel{ private int n,nrM; private Desen5 canvas; private JButton adauga; private JTextField fisier; private Set p,m; private AscultatorButon ab; public Problema5(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); canvas=new Desen5(); adauga=new JButton("Citeste"); adauga.addActionListener(ab); fisier=new JTextField(10); fisier.setText("prob5.txt"); JPanel p=new JPanel(); JPanel p3=new JPanel(new GridLayout(2,1)); JPanel p1=new JPanel(); p1.add(adauga); JPanel p2=new JPanel(); p2.add(fisier); p3.add(p1); p3.add(p2); p.add(p3); this.add(p,BorderLayout.WEST); this.add(canvas,BorderLayout.CENTER); }//Problema3() --> constructorul "clasei" public class AscultatorButon implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==adauga){

Page 47: Laborator geometrie computationala

2

String s=fisier.getText(); p=new TreeSet(); m=new TreeSet(); try{ StreamTokenizer st=new StreamTokenizer(new BufferedReader(new FileReader(s))); st.nextToken(); nrM=(int)st.nval; for(int i=0;i<nrM;++i){ st.nextToken(); int x=(int)st.nval; st.nextToken(); int y=(int)st.nval; Punct5 punct1=new Punct5(x,y); p.add(punct1); st.nextToken(); int x1=(int)st.nval; st.nextToken(); int y1=(int)st.nval; Punct5 punct2=new Punct5(x1,y1); p.add(punct2); st.nextToken(); int f1=(int)st.nval; st.nextToken(); int f2=(int)st.nval; st.nextToken(); int pp1=(int)st.nval; st.nextToken(); int pp2=(int)st.nval; m.add(new Muchie5(i, punct1, punct2, f1, f2, pp1, pp2)); } } catch(FileNotFoundException fnfe){ JOptionPane.showMessageDialog(null,"Nu exista acest fisier!","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } catch(IOException eeee){ System.out.println("Probleme la introducerea datelor!!!!!!!!!!!!"); } Punct5[] pp=new Punct5[p.size()]; System.out.println("Numarul de puncte : "+p.size()); Iterator it=p.iterator(); for(int i=0;i<p.size();++i){ pp[i]=(Punct5)(it.next()); } Muchie5[] mm=new Muchie5[m.size()]; it=m.iterator(); for(int i=0;i<m.size();++i){ mm[i]=(Muchie5)(it.next());

Page 48: Laborator geometrie computationala

3

} Algoritm5 a=new Algoritm5(pp,mm); canvas.start(a); } } }//actionPerformed() } Algoritmul pt. problema 5 package prob5; import java.util.*; public class Algoritm5{ private Punct5[] a; private Muchie5[] dcel; private List matura; //maturatorul //private int[] W; //vectorul care retine ponderile muchiilor; private double x,y; private Muchie5 mBin; private ArrayList lantBin; private ArrayList[] L; private TreeSet OUT1; private int mijloc=0; public Algoritm5(Punct5[] a,Muchie5[] dcel){ this.a=a; this.dcel=dcel; Arrays.sort(a); interschimba(); listeaza(); calculeazaPonderi(); gasesteLanturi(); } /*** private void sorteaza(){ for(int i=0;i<a.length-1;++i) for(int j=i+1;j<a.length;++j){ if(a[i].getY()>a[j].getY()){ Punct3 aux; aux=a[i]; a[i]=a[j]; a[j]=aux; }

Page 49: Laborator geometrie computationala

4

} } ***/ private void interschimba(){ for(int i=0;i<dcel.length;++i){ if(((dcel[i].getV1()).compareTo(dcel[i].getV2()))>0) dcel[i].inverseaza(); } } public void listeaza(){ for(int i=0;i<a.length;++i){ TreeSet c=new TreeSet(); TreeSet b=new TreeSet(); for(int j=0;j<dcel.length;++j){ if(((dcel[j].getV1()).compareTo(a[i]))==0) c.add(dcel[j]); if(((dcel[j].getV2()).compareTo(a[i]))==0) b.add(dcel[j]); } Iterator it=c.iterator(); System.out.print("+"+(i+1)+"\t:"); while(it.hasNext()) System.out.print(" "+((Muchie5)(it.next())).getNr()); System.out.println(); a[i].setC(c);//muchiile care ies it=b.iterator(); System.out.print("-"+(i+1)+"\t:"); while(it.hasNext()) System.out.print(" "+((Muchie5)(it.next())).getNr()); System.out.println(); a[i].setB(b); //muchiile care intra for(int l=0;l<dcel.length;++l){ if(dcel[l].getV1().compareTo(a[i])==0) { dcel[l].getV1().setB(b); dcel[l].getV1().setC(c); } if(dcel[l].getV2().compareTo(a[i])==0) { dcel[l].getV2().setB(b); dcel[l].getV2().setC(c); } } }

Page 50: Laborator geometrie computationala

5

} public void calculeazaPonderi(){ for(int i=0;i<dcel.length;++i) dcel[i].setW(1); for(int i=1;i<a.length-1;++i){ TreeSet IN=(TreeSet)(a[i].getB()); int wIn=0; Iterator it=IN.iterator(); while(it.hasNext()){ Muchie5 muchie=(Muchie5)(it.next()); wIn+=muchie.getW(); } TreeSet OUT=(TreeSet)(a[i].getC()); int wOut=0; it=OUT.iterator(); while(it.hasNext()){ Muchie5 muchie=(Muchie5)(it.next()); wOut+=muchie.getW(); } if (wIn>wOut) { it=OUT.iterator(); Muchie5 muchie=(Muchie5)(it.next()); muchie.setW(1+wIn-wOut); } } for(int i=a.length-1;i>0;--i){ TreeSet IN=(TreeSet)(a[i].getB()); int wIn=0; Iterator it=IN.iterator(); while(it.hasNext()){ Muchie5 muchie=(Muchie5)(it.next()); wIn+=muchie.getW(); } TreeSet OUT=(TreeSet)(a[i].getC()); int wOut=0; it=OUT.iterator(); while(it.hasNext()){ Muchie5 muchie=(Muchie5)(it.next()); wOut+=muchie.getW(); }

Page 51: Laborator geometrie computationala

6

if (wIn<wOut) { it=IN.iterator(); Muchie5 muchie=(Muchie5)(it.next()); muchie.setW(muchie.getW()+wOut-wIn); } } //afisare poderi muchii System.out.println("---***---...afisare ponderi muchii...---***---"); for(int i=0;i<dcel.length;++i){ System.out.println(dcel[i].getNr()+":\t"+dcel[i].getW()); } } public void gasesteLanturi(){ int nr=0; TreeSet OUT=(TreeSet)a[0].getC(); Iterator it=OUT.iterator(); while(it.hasNext()){ nr+=((Muchie5)(it.next())).getW(); } L=new ArrayList[nr]; System.out.println("nr= "+nr); it=OUT.iterator(); int i=-1; while(it.hasNext()){ Muchie5 muchie=(Muchie5)(it.next()); while(muchie.getW()!=0){ ++i; L[i]=new ArrayList(); Lant(muchie,i); } } System.out.println("+++***---Afisarea lanturilor---***+++"); for(int k=0;k<nr;++k){ System.out.print("start"); Object[] array=L[k].toArray(); for(int j=0;j<array.length;++j) System.out.print(" --> "+((Muchie5)array[j]).getNr()); System.out.println(); } }

Page 52: Laborator geometrie computationala

7

public void Lant(Muchie5 muchie,int i){ L[i].add(muchie); muchie.setW(muchie.getW()-1); if(muchie.getV2().compareTo(a[a.length-1])==0) return; else{ //if(muchie.getW()!=0){ //System.out.println(muchie.getNr()); Punct5 nextPunct=(Punct5)(muchie.getV2()); TreeSet o=(TreeSet)(nextPunct.getC()); //OUT1=(TreeSet)(((Punct5)(muchie.getV2())).getC()); //if (o==null) { // System.out.println("M-am oprit la :"+((Punct5)(muchie.getV2())).getX()+" "+((Punct5)(muchie.getV2())).getY()); //} Iterator it=o.iterator(); Muchie5 muchie1=null; while(it.hasNext()){ muchie1=(Muchie5)(it.next()); if(muchie1.getW()>0) break; } Lant(muchie1,i); } } public int cautaLantul(double x, double y){ this.x=x; this.y=y; cautaBinar(0,L.length-1); Object[] array=lantBin.toArray(); Punct5[] array1=new Punct5[array.length+1]; for(int i=0;i<array.length;++i){ array1[i]=((Muchie5)array[i]).getV1(); } Muchie5 muchie=null; array1[array1.length-1]=((Muchie5)array[array.length-1]).getV2(); Punct5 punctCautat=new Punct5(x,y); int nr=Math.abs(Arrays.binarySearch(array1,punctCautat)); System.out.println("Punct de insertie "+nr); if (nr<=1) {muchie=(Muchie5)array[0];} if (nr>=array1.length) muchie=(Muchie5)array[array.length-1]; if ((nr>1)&&(nr<array1.length)) {muchie=(Muchie5)array[nr-2];} System.out.println(nr); mBin=muchie; int F=0; if(mBin==null) return 0; if(calcDet (mBin.getV1().getX(),

Page 53: Laborator geometrie computationala

8

mBin.getV1().getY(), x, y, mBin.getV2().getX(), mBin.getV2().getY() )>0) F=mBin.getF2(); else F=mBin.getF1(); System.out.println(mBin.getNr()+"Numarul muchiei"); return F; } public Muchie5 getMuchie(){ return mBin; } public ArrayList []getLant(){ ArrayList[] array=new ArrayList[2]; array[0]=lantBin; if(calcDet (mBin.getV1().getX(), mBin.getV1().getY(), x, y, mBin.getV2().getX(), mBin.getV2().getY() )>0) array[1]=L[mijloc+1]; else array[1]=L[mijloc-1]; return array; } public ArrayList[] getListaLanturi(){ return L; } public ArrayList getUnLant(){ return lantBin; } public void cautaBinar(int s,int d){ int mijloc=(s+d)/2; if(s<d){ Object[] array=L[mijloc].toArray(); Punct5[] array1=new Punct5[array.length+1]; for(int i=0;i<array.length;++i){ array1[i]=((Muchie5)array[i]).getV1();

Page 54: Laborator geometrie computationala

9

} array1[array1.length-1]=((Muchie5)array[array.length-1]).getV2(); Punct5 punctCautat=new Punct5(x,y); int nr=Math.abs(Arrays.binarySearch(array1,punctCautat)); Muchie5 muchie=null; if (nr<=1) muchie=(Muchie5)array[0]; if (nr>=array1.length) muchie=(Muchie5)array[array.length-1]; if ((nr>1)&&(nr<array1.length)) muchie=(Muchie5)array[nr-2]; if (calcDet(muchie.getV1().getX(), muchie.getV1().getY(), x, y, muchie.getV2().getX(), muchie.getV2().getY())>0) cautaBinar(mijloc+1,d); else cautaBinar(s,mijloc-1); } else { lantBin=L[mijloc]; System.out.println("mijlocul este"+mijloc); this.mijloc=mijloc; } } public int cautaLespede(double x, double y){ return ~Arrays.binarySearch(a,new Punct5(x,y)); } //intoarce in a cata lespede se afla punctul cautat public double calcDet(double x1,double y1, double x2, double y2, double x3, double y3){ return x1*y2+x2*y3+x3*y1-y2*x3-y3*x1-y1*x2; } public Muchie5[] getDCEL(){ return dcel; } } Suprafata de desen pentru problema 5 package prob5; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Graphics.*;

Page 55: Laborator geometrie computationala

10

import java.awt.Graphics2D.*; import java.awt.geom.*; import java.util.*; public class Desen5 extends Canvas{ private boolean pornesteMouse=false,mouseSelect=false; private int[] x,y; private Color[] culoare; private int mouseX,mouseY,cc,n=0,nn,MMx,MMy; private double Mx,MMMx; private double My,MMMy; private JTextField output; // private Punct[] punct; private int centrux,centruy,F; private Algoritm5 a; public Desen5(){ this.setSize(500,500); } public void start(Algoritm5 a){ pornesteMouse=true; this.a=a; repaint(); } public boolean mouseDown(Event e,int x,int y){ if (pornesteMouse) { mouseX=x; mouseY=y; Mx=(mouseX-250)/10.0; My=(250-mouseY)/10.0; F=a.cautaLantul(Mx,My); System.out.println("F este: "+F); mouseSelect=true; repaint(); } return true; } public void paint(Graphics g){ Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.BLACK); g2.drawLine(0,0,500,0); g2.drawLine(0,0,0,500); g2.drawLine(500,0,500,500); g2.drawLine(0,500,500,500);

Page 56: Laborator geometrie computationala

11

//trasez axa x' g2.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g2.drawString("x",calcX(21),calcY(-1)); g2.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g2.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y' g2.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20)); g2.drawString("y",calcX(-1),calcY(21)); g2.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g2.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); Muchie5[] dcel; if(a!=null) dcel=a.getDCEL(); else dcel=new Muchie5[0]; for(int i=0;i<dcel.length;++i){ g2.drawLine(calcX((int)dcel[i].getV1().getX()), calcY((int)dcel[i].getV1().getY()), calcX((int)dcel[i].getV2().getX()), calcY((int)dcel[i].getV2().getY()) ); int mijlocX=(calcX((int)dcel[i].getV1().getX())+calcX((int)dcel[i].getV2().getX()))/2; int mijlocY=(calcY((int)dcel[i].getV1().getY())+calcY((int)dcel[i].getV2().getY()))/2; g2.fillOval(calcX((int)dcel[i].getV1().getX())-2,calcY((int)dcel[i].getV1().getY())-2,4,4); g2.fillOval(calcX((int)dcel[i].getV2().getX())-2,calcY((int)dcel[i].getV2().getY())-2,4,4); //desenatul arcelor corect!!!!! double mX=(dcel[i].getV1().getX()+dcel[i].getV2().getX())/2.0; double mY=(dcel[i].getV1().getY()+dcel[i].getV2().getY())/2.0; double mmX=mX; double mmY=mY; double mp1X=mX-0.3; double mp1Y=mY-0.3; double mp2X=mX+0.3; double mp2Y=mY-0.3; double inclinare=0d;

Page 57: Laborator geometrie computationala

12

if(dcel[i].getV1().getX()<dcel[i].getV2().getX()) inclinare=-Math.PI/2; else inclinare=Math.PI/2; g2.setColor(Color.red); //double teta=(Math.sqrt(Math.pow(dcel[i].getV1().getX()-mX,2)+Math.pow(dcel[i].getV1().getY()-mY,2)))/(Math.sqrt(Math.pow(dcel[i].getV1().getY()-mY,2))); double cosu=Math.sqrt(Math.cos(Math.pow(dcel[i].getV1().getY()-mY,2)))/Math.sqrt((Math.pow(dcel[i].getV1().getX()-mX,2))); double tangenta=(dcel[i].getV2().getY()-dcel[i].getV1().getY())/(dcel[i].getV2().getX()-dcel[i].getV1().getX()); double teta=Math.atan(tangenta)+inclinare; AffineTransform at = new AffineTransform(); mX-=mp1X; mY-=mp1Y; at.rotate(teta,mX,mY); double newX = at.getTranslateX(); double newY = at.getTranslateY(); newX+=mp1X; newY+=mp1Y; g2.drawLine(mijlocX,mijlocY,calcX(newX),calcY(newY)); AffineTransform at1 = new AffineTransform(); mmX-=mp2X; mmY-=mp2Y; at1.rotate(teta,mmX,mmY); newX = at1.getTranslateX(); newY = at1.getTranslateY(); newX+=mp2X; newY+=mp2Y; g2.drawLine(mijlocX,mijlocY,calcX(newX),calcY(newY)); //g2.drawLine(mijlocX,mijlocY,mijlocX+3,mijlocY+3); //g2.drawLine(mijlocX,mijlocY,mijlocX-3,mijlocY+3); g2.drawString(""+dcel[i].getNr(),mijlocX+2,mijlocY+2);

Page 58: Laborator geometrie computationala

13

g2.setColor(Color.black); } if(mouseSelect){ g.setColor(Color.blue); g.fillOval(mouseX,mouseY,1,1); g.drawLine(mouseX+2,mouseY-2,mouseX-2,mouseY+2); g.drawLine(mouseX-2,mouseY-2,mouseX+2,mouseY+2); dcel=a.getDCEL(); for(int i=0;i<dcel.length;++i){ if((dcel[i].getF1()==F)||(dcel[i].getF2()==F)) g.drawLine(calcX((int)dcel[i].getV1().getX()), calcY((int)dcel[i].getV1().getY()), calcX((int)dcel[i].getV2().getX()), calcY((int)dcel[i].getV2().getY()) ); } /*** if (F==0) { ArrayList sir3 = a.getUnLant(); g.setColor(Color.YELLOW); Object[] sir4=sir3.toArray(); for(int d=0;d<sir4.length;++d){ g.drawLine(calcX((int)((Muchie5)sir4[d]).getV1().getX())+2, calcY((int)((Muchie5)sir4[d]).getV1().getY())+2, calcX((int)((Muchie5)sir4[d]).getV2().getX())+2, calcY((int)((Muchie5)sir4[d]).getV2().getY())+2 ); } } else{ ArrayList[] array=a.getLant(); g.setColor(Color.YELLOW); Object[] sir1=array[0].toArray(); for(int d=0;d<sir1.length;++d){ g.drawLine(calcX((int)((Muchie5)sir1[d]).getV1().getX())+2, calcY((int)((Muchie5)sir1[d]).getV1().getY())+2, calcX((int)((Muchie5)sir1[d]).getV2().getX())+2, calcY((int)((Muchie5)sir1[d]).getV2().getY())+2

Page 59: Laborator geometrie computationala

14

); } g.setColor(Color.pink); Object[] sir2=array[1].toArray(); for(int d=0;d<sir2.length;++d){ g.drawLine(calcX((int)((Muchie5)sir2[d]).getV1().getX())-2, calcY((int)((Muchie5)sir2[d]).getV1().getY())-2, calcX((int)((Muchie5)sir2[d]).getV2().getX())-2, calcY((int)((Muchie5)sir2[d]).getV2().getY())-2 ); } g.setColor(Color.blue); } ***/ //era prea oribil } } public int calcX(double x){ return (int)(250+x*10); }//calcX(int x) public int calcY(double y){ return (int)(250-y*10); }//calcY(int y) } Clasa ajutatoare. Creaza obiecte de tip Punct5 package prob6; import java.util.*; public class Punct5 implements Comparable{ private double x,y; private Set b,c; public Punct5(double x,double y){ this.x=x; this.y=y; }

Page 60: Laborator geometrie computationala

15

public double getX(){ return x; } public double getY(){ return y; } public int compareTo(Object o){ if(y>((Punct5)o).getY()) return 255; if((y==((Punct5)o).getY())&&(x==((Punct5)o).getX())) return 0; return -256; } public void setC(TreeSet c){ this.c=c; } public void setB(TreeSet b){ this.b=b; } public Set getC(){ return c; } public Set getB(){ return b; } } Clasa ajutatoare. Creaza obiecte de tip Muchie package prob5; public class Muchie5 implements Comparable{ private int nr,f1,f2,p1,p2,pondere; private Punct5 v1,v2; public Muchie5(int nr, Punct5 v1, Punct5 v2, int f1, int f2, int p1, int p2){ this.nr=nr; this.v1=v1; this.v2=v2; this.f1=f1; this.f2=f2; this.p1=p1; this.p2=p2;

Page 61: Laborator geometrie computationala

16

} public int getNr(){ return nr; } public Punct5 getV1(){ return v1; } public Punct5 getV2(){ return v2; } public int getF1(){ return f1; } public int getF2(){ return f2; } public int getP1(){ return p1; } public int getP2(){ return p2; } public int compareTo(Object o){ Muchie5 m=(Muchie5)o; if (f1==0) return -256; if (f2==0) return +255; if ((v1.equals(m.getV1()))&&(v2.equals(m.getV2()))) return 0; if (f2==m.getF1()) return -256; return 255; } public void inverseaza(){ Punct5 paux; paux=v1; v1=v2; v2=paux; f1=f1+f2;

Page 62: Laborator geometrie computationala

17

f2=f1-f2; f1=f1-f2; p1=p1+p2; p2=p1-p2; p2=p1-p2; } public void setW(int nr){ pondere=nr; } public int getW(){ return pondere; } }

Page 63: Laborator geometrie computationala

1

Laborator 6

Se da N puncte fixe. Si apoi se introduc M Dreptunghiuri. Sa se numere cate puncte se afla in interiorul dreptunghiului prin Principiul Includerii si Excluderii. package prob6; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class Problema6 extends JPanel{ private Desen6 canvas; private JButton b1,b2,b3; private JTextField fisier; private Set p,m; private AscultatorButon ab; public Problema6(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); canvas=new Desen6(); b1=new JButton("1"); b1.addActionListener(ab); b2=new JButton("2"); b2.addActionListener(ab); b3=new JButton("3"); b3.addActionListener(ab); fisier=new JTextField(10); fisier.setText("prob5.txt"); fisier.setVisible(false); JPanel p=new JPanel(); JPanel p3=new JPanel(new GridLayout(2,1)); JPanel p1=new JPanel(); p1.add(b1); p1.add(b2); p1.add(b3); JPanel p2=new JPanel(); p2.add(fisier); p3.add(p1);

Page 64: Laborator geometrie computationala

2

p3.add(p2); p.add(p3); this.add(p,BorderLayout.WEST); this.add(canvas,BorderLayout.CENTER); }//Problema3() --> constructorul "clasei" public class AscultatorButon implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==b1) { canvas.set(1); } if(e.getSource()==b2) { canvas.set(2); } if(e.getSource()==b3) { canvas.set(3); } }//actionPerformed() } } Algoritmul pt. problema 6 package prob6; import java.util.*; public class Algoritm6{ private ArrayList al;//multimea punctelor private TreeSet list1; private Punct6[] list2; private int[][] mat; public Algoritm6 (ArrayList al){ this.al=al; makeList1(); makeList2(); mat=new int[list2.length+1][list2.length+1]; makeMat(); } public void makeList1() { Object[] aux=al.toArray(); list1=new TreeSet(); for(int i=0;i<aux.length;++i)

Page 65: Laborator geometrie computationala

3

list1.add(aux[i]); } public void makeList2() { Object[] aux=al.toArray(); list2=new Punct6[aux.length]; for(int i=0;i<aux.length;++i) list2[i]=(Punct6)aux[i]; qSort(0,aux.length-1); } public void qSort(int s,int d) { int i=s; int j=d; Punct6 m=list2[(i+j)/2]; Punct6 aux; do { while(list2[i].getY()<m.getY()) i++; while(list2[j].getY()>m.getY()) j--; if(i<=j) { aux=list2[i]; list2[i]=list2[j]; list2[j]=aux; i++; j--; } } while(i<=j); if(s<j) qSort(s,j); if(d>i) qSort(i,d); } public void makeMat() { Iterator it=list1.iterator(); int i=0; for(int j=0;j<list2.length;++j) { System.out.println(j+" -> "+list2[j].getX()+" "+list2[j].getY()); } while(it.hasNext()) { Punct6 punct=(Punct6)(it.next()); ++i; int poz=cautareBinara(0,list2.length-1,punct)+1; System.out.println(punct.getX()+" "+punct.getY()+" "+(poz-1)); for(int j=0;j<poz;++j) mat[i][j]=mat[i-1][j]; for(int j=poz;j<mat.length;++j) mat[i][j]=mat[i-1][j]+1;

Page 66: Laborator geometrie computationala

4

} } public int cautareBinara(int s, int d, Punct6 punct) { int nr=(s+d)/2; if((list2[nr].getX()==punct.getX())&&(punct.getY()==list2[nr].getY())) return nr; if(s<d) { boolean ok=true; //if((list2[nr].getY()==punct.getY())&&(list2[nr].getX()>punct.getX())) ok=false; if(list2[nr].getY()<punct.getY()) ok=false; if(ok) return cautareBinara(s,nr-1,punct); else return cautareBinara(nr+1,d,punct); } return -256; } public int getW(Punct6 punct) { System.out.println("Afisare matrice"); for(int i=0;i<mat.length;++i) { System.out.println(); for(int j=0;j<mat[0].length;++j) System.out.print(mat[i][j]+"\t"); } Punct6[] puncte=new Punct6[list2.length]; Iterator it=list1.iterator(); int k=0; Punct6p[] puncte1=new Punct6p[list2.length]; System.out.println(); while(it.hasNext()) { Punct6 p=(Punct6)(it.next()); puncte[k]=p; puncte1[k]=new Punct6p(p.getX(),p.getY()); k++; System.out.println((k-1)+" -> "+puncte[k-1].getX()+" "+puncte[k-1].getY()); } Arrays.sort(puncte1); int poz=~Arrays.binarySearch(puncte,punct); int poz1=~Arrays.binarySearch(puncte1,new Punct6p(punct.getX(),punct.getY()));

Page 67: Laborator geometrie computationala

5

System.out.println("Punctul : "+punct.getX()+" "+punct.getY()+" poz: "+poz+" "+poz1); if(poz<0) { System.out.println("err "+poz); poz=~poz; } if(poz1<0) { System.out.println("err "+poz1); poz1=~poz1; } /* if(poz==0) { System.out.println("pozitia 0"); return 0; } */ if((poz1==0)||(poz==0)) { return mat[poz][poz1]; } int nrc=cautareBinara(0,list2.length-1,puncte[poz-1]); System.out.println(nrc); int nr=mat[poz][poz1]; System.out.println("nr de puncte este: "+nr); if (nr<0) { System.out.println("Mi-a dat numarul = "+nr); return -256; } return nr; } } Suprafata de desen pentru problema 6 package prob6; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Graphics.*; import java.util.*; public class Desen6 extends Canvas { private boolean mouseIntroducereDate=true; private ArrayList al;

Page 68: Laborator geometrie computationala

6

private Algoritm6 a; private double mx,my,min,max; private int contor=0,nr=0,dragX,dragY; private Vector x,y,x1,y1,x2,y2; private boolean hand=false, normal=true,desenat=false,drag=false, resize=false, dragN=false, dragS=false, dragW=false, dragE=false; public Desen6(){ al=new ArrayList(); this.enableEvents(MouseEvent.MOUSE_CLICKED); this.enableEvents(MouseEvent.MOUSE_DRAGGED); this.enableEvents(MouseEvent.MOUSE_RELEASED); this.enableEvents(MouseEvent.MOUSE_MOVED); this.enableEvents(506); //MouseEvent.MOUSE_PRESSED setSize(500,500); x=new Vector(); y=new Vector(); x1=new Vector(); y1=new Vector(); //setCursor(new Cursor(Cursor.HAND_CURSOR)); } public void paint(Graphics g) { g.setColor(Color.BLACK); g.drawLine(0,0,500,0); g.drawLine(0,0,0,500); g.drawLine(500,0,500,500); g.drawLine(0,500,500,500); //trasez axa x' g.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g.drawString("x",calcX(21),calcY(-1)); g.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y' g.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20)); g.drawString("y",calcX(-1),calcY(21)); g.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); Object[] puncte=al.toArray(); for(int i=0;i<puncte.length;++i) { Punct6 punct=(Punct6)puncte[i]; g.fillOval(calcX(punct.getX())-2, calcY(punct.getY())-2,4,4);

Page 69: Laborator geometrie computationala

7

} if(x1.size()==1) { g.setColor(Color.red); g.fillOval(((Integer) (x1.elementAt(0))).intValue()-2,((Integer) (y1.elementAt(0))).intValue()-2,4,4); g.setColor(Color.black); } if(x1.size()==2) { g.setColor(Color.red); g.fillOval(((Integer) (x1.elementAt(0))).intValue()-2,((Integer) (y1.elementAt(0))).intValue()-2,4,4); g.fillOval(((Integer) (x1.elementAt(1))).intValue()-2,((Integer) (y1.elementAt(1))).intValue()-2,4,4); g.setColor(Color.black); } if(x1.size()==4){ g.drawLine( ((Integer) (x1.elementAt(0))).intValue(), ((Integer) (y1.elementAt(0))).intValue(), ((Integer) (x1.elementAt(2))).intValue(), ((Integer) (y1.elementAt(2))).intValue()); g.drawLine( ((Integer) (x1.elementAt(1))).intValue(), ((Integer) (y1.elementAt(1))).intValue(), ((Integer) (x1.elementAt(3))).intValue(), ((Integer) (y1.elementAt(3))).intValue()); g.drawLine( ((Integer) (x1.elementAt(0))).intValue(), ((Integer) (y1.elementAt(0))).intValue(), ((Integer) (x1.elementAt(3))).intValue(), ((Integer) (y1.elementAt(3))).intValue()); g.drawLine( ((Integer) (x1.elementAt(2))).intValue(), ((Integer) (y1.elementAt(2))).intValue(), ((Integer) (x1.elementAt(1))).intValue(), ((Integer) (y1.elementAt(1))).intValue()); g.setColor(Color.RED); g.fillOval(((Integer) (x1.elementAt(0))).intValue()-2,((Integer) (y1.elementAt(0))).intValue()-2,4,4); g.fillOval(((Integer) (x1.elementAt(1))).intValue()-2,((Integer) (y1.elementAt(1))).intValue()-2,4,4); g.fillOval(((Integer) (x1.elementAt(2))).intValue()-2,((Integer) (y1.elementAt(2))).intValue()-2,4,4); g.fillOval(((Integer) (x1.elementAt(3))).intValue()-2,((Integer) (y1.elementAt(3))).intValue()-2,4,4); //System.out.println(b.size()); int nr1=a.getW(new Punct6(((((Integer) (x1.elementAt(0))).intValue()-250)/10.0),(250-((Integer)(y1.elementAt(0))).intValue())/10.0)); int nr2=a.getW(new Punct6(((((Integer) (x1.elementAt(1))).intValue()-250)/10.0),(250-((Integer)(y1.elementAt(1))).intValue())/10.0));

Page 70: Laborator geometrie computationala

8

int nr3=a.getW(new Punct6(((((Integer) (x1.elementAt(2))).intValue()-250)/10.0),(250-((Integer)(y1.elementAt(2))).intValue())/10.0)); int nr4=a.getW(new Punct6(((((Integer) (x1.elementAt(3))).intValue()-250)/10.0),(250-((Integer)(y1.elementAt(3))).intValue())/10.0)); //if(nr==1) g.drawString("Este un punct",240,440); g.drawString("Sunt "+Math.abs(nr3-nr1-nr2+nr4)+" puncte",240,440); nr=0; desenat=true; contor=0; x2=x1; y2=y1; x1=new Vector(); y1=new Vector(); //mousePrim=false; } } public int calcX(double x){ return (int)(250+x*10); }//calcX(int x) public int calcY(double y){ return (int)(250-y*10); }//calcY(int y) public void processMouseEvent(MouseEvent e) { if((normal)&&(e.getID()==MouseEvent.MOUSE_CLICKED)){ if((mouseIntroducereDate)&&(e.getButton()==MouseEvent.BUTTON3)) { mouseIntroducereDate=false; a=new Algoritm6(al); } if((mouseIntroducereDate)&&(e.getButton()==MouseEvent.BUTTON1)) { al.add(new Punct6((e.getX()-250)/10.0,(250-e.getY())/10.0)); repaint(); } if((!mouseIntroducereDate)&&(e.getButton()==MouseEvent.BUTTON1)) { contor++; int mouseX=e.getX(); int mouseY=e.getY(); mx=(mouseX-250)/10.0; my=(250-mouseY)/10.0; contor=x1.size(); System.out.println(contor); x1.add(mouseX); y1.add(mouseY);

Page 71: Laborator geometrie computationala

9

if(x1.size()==2) { mx=Math.max(((Integer)(x1.elementAt(0))).intValue(),((Integer)(x1.elementAt(1))).intValue()); my=Math.min(((Integer)(y1.elementAt(0))).intValue(),((Integer)(y1.elementAt(1))).intValue()); x1.add((int)mx); y1.add((int)my); mx=Math.min(((Integer)(x1.elementAt(0))).intValue(),((Integer)(x1.elementAt(1))).intValue()); my=Math.max(((Integer)(y1.elementAt(0))).intValue(),((Integer)(y1.elementAt(1))).intValue()); x1.add((int)mx); y1.add((int)my); if(((Integer)(x1.elementAt(0))).intValue()> ((Integer)(x1.elementAt(1))).intValue() ) { Object o=x1.elementAt(0); x1.setElementAt(x1.elementAt(1),0); x1.setElementAt(o,1); o=y1.elementAt(0); y1.setElementAt(y1.elementAt(1),0); y1.setElementAt(o,1); //b=new boolean[x.size()]; } } System.out.println("aci"); repaint(); } } } public void processMouseMotionEvent(MouseEvent e){ if((hand)&&(e.getID()==MouseEvent.MOUSE_DRAGGED)&&(!drag)){ System.out.println("mous pointat "+e.getX()+" "+e.getY()); boolean ok=false; if((((Integer)(x2.elementAt(0))).intValue()<e.getX())&& (((Integer)(y2.elementAt(0))).intValue()<e.getY())&& (((Integer)(x2.elementAt(1))).intValue()>e.getX())&& (((Integer)(y2.elementAt(1))).intValue()>e.getY())&& (((Integer)(x2.elementAt(2))).intValue()>e.getX())&& (((Integer)(y2.elementAt(2))).intValue()<e.getY())&& (((Integer)(x2.elementAt(3))).intValue()<e.getX())&& (((Integer)(y2.elementAt(3))).intValue()>e.getY())) {

Page 72: Laborator geometrie computationala

10

drag=true; dragX=e.getX(); dragY=e.getY(); } } if((drag)&&(e.getID()==MouseEvent.MOUSE_DRAGGED)){ System.out.println("mous tras "+e.getX()+" "+e.getY()); x1.add(((Integer)(x2.elementAt(0))).intValue()+e.getX()-dragX); x1.add(((Integer)(x2.elementAt(1))).intValue()+e.getX()-dragX); x1.add(((Integer)(x2.elementAt(2))).intValue()+e.getX()-dragX); x1.add(((Integer)(x2.elementAt(3))).intValue()+e.getX()-dragX); y1.add(((Integer)(y2.elementAt(0))).intValue()+e.getY()-dragY); y1.add(((Integer)(y2.elementAt(1))).intValue()+e.getY()-dragY); y1.add(((Integer)(y2.elementAt(2))).intValue()+e.getY()-dragY); y1.add(((Integer)(y2.elementAt(3))).intValue()+e.getY()-dragY); x2.setElementAt(x1.elementAt(0),0); x2.setElementAt(x1.elementAt(1),1); x2.setElementAt(x1.elementAt(2),2); x2.setElementAt(x1.elementAt(3),3); y2.setElementAt(y1.elementAt(0),0); y2.setElementAt(y1.elementAt(1),1); y2.setElementAt(y1.elementAt(2),2); y2.setElementAt(y1.elementAt(3),3); dragX=e.getX(); dragY=e.getY(); repaint(500); } else drag=false; if((resize)&&(e.getID()==MouseEvent.MOUSE_MOVED)) { //System.out.println("mous mutat "+e.getX()+" "+e.getY()); int X0=((Integer)(x2.elementAt(0))).intValue(); int X1=((Integer)(x2.elementAt(1))).intValue(); int X2=((Integer)(x2.elementAt(2))).intValue(); int X3=((Integer)(x2.elementAt(3))).intValue(); int Y0=((Integer)(y2.elementAt(0))).intValue(); int Y1=((Integer)(y2.elementAt(1))).intValue(); int Y2=((Integer)(y2.elementAt(2))).intValue(); int Y3=((Integer)(y2.elementAt(3))).intValue(); if((e.getX()>X0)&&(Math.abs(e.getY()-Y0)<5)&& (e.getX()<X2)&&(Math.abs(e.getY()-Y2)<5)) { setCursor(new Cursor(Cursor.N_RESIZE_CURSOR)); dragX=e.getX(); dragY=e.getY(); dragN=true;

Page 73: Laborator geometrie computationala

11

} else { dragN=false; } if((e.getX()>X3)&&(Math.abs(e.getY()-Y3)<5)&& (e.getX()<X1)&&(Math.abs(e.getY()-Y1)<5)) { setCursor(new Cursor(Cursor.S_RESIZE_CURSOR)); dragX=e.getX(); dragY=e.getY(); dragS=true; } else { dragS=false; } if((e.getY()>Y0)&&(Math.abs(e.getX()-X0)<5)&& (e.getY()<Y3)&&(Math.abs(e.getX()-X3)<5)) { setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); dragX=e.getX(); dragY=e.getY(); dragW=true; } else { dragW=false; } if((e.getY()>Y2)&&(Math.abs(e.getX()-X2)<5)&& (e.getY()<Y1)&&(Math.abs(e.getX()-X1)<5)) { setCursor(new Cursor(Cursor.E_RESIZE_CURSOR)); dragX=e.getX(); dragY=e.getY(); dragE=true; } else { dragE=false; } if((!dragN)&&(!dragS)&&(!dragW)&&(!dragE)) setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } if((resize)&&(e.getID()==MouseEvent.MOUSE_DRAGGED)) { if(dragN) { int X0=((Integer)(x2.elementAt(0))).intValue(); int X2=((Integer)(x2.elementAt(2))).intValue(); int Y0=((Integer)(y2.elementAt(0))).intValue(); int Y2=((Integer)(y2.elementAt(2))).intValue(); //x2.setElementAt(X0+e.getX()-dragX,0); //x2.setElementAt(x1.elementAt(1),1); //x2.setElementAt(X2+e.getX()-dragX,2); //x2.setElementAt(x1.elementAt(3),3); y2.setElementAt(Y0+e.getY()-dragY,0);

Page 74: Laborator geometrie computationala

12

//y2.setElementAt(y1.elementAt(1),1); y2.setElementAt(Y2+e.getY()-dragY,2); //y2.setElementAt(y1.elementAt(3),3); x1.add(((Integer)(x2.elementAt(0))).intValue()); x1.add(((Integer)(x2.elementAt(1))).intValue()); x1.add(((Integer)(x2.elementAt(2))).intValue()); x1.add(((Integer)(x2.elementAt(3))).intValue()); y1.add(((Integer)(y2.elementAt(0))).intValue()); y1.add(((Integer)(y2.elementAt(1))).intValue()); y1.add(((Integer)(y2.elementAt(2))).intValue()); y1.add(((Integer)(y2.elementAt(3))).intValue()); dragX=e.getX(); dragY=e.getY(); repaint(50); } else dragN=false; if(dragS) { int X3=((Integer)(x2.elementAt(3))).intValue(); int X1=((Integer)(x2.elementAt(1))).intValue(); int Y3=((Integer)(y2.elementAt(3))).intValue(); int Y1=((Integer)(y2.elementAt(1))).intValue(); //x2.setElementAt(X3+e.getX()-dragX,3); //x2.setElementAt(x1.elementAt(1),1); //x2.setElementAt(X1+e.getX()-dragX,1); //x2.setElementAt(x1.elementAt(3),3); y2.setElementAt(Y3+e.getY()-dragY,3); //y2.setElementAt(y1.elementAt(1),1); y2.setElementAt(Y3+e.getY()-dragY,1); //y2.setElementAt(y1.elementAt(3),3); x1.add(((Integer)(x2.elementAt(0))).intValue()); x1.add(((Integer)(x2.elementAt(1))).intValue()); x1.add(((Integer)(x2.elementAt(2))).intValue()); x1.add(((Integer)(x2.elementAt(3))).intValue()); y1.add(((Integer)(y2.elementAt(0))).intValue()); y1.add(((Integer)(y2.elementAt(1))).intValue()); y1.add(((Integer)(y2.elementAt(2))).intValue()); y1.add(((Integer)(y2.elementAt(3))).intValue()); dragX=e.getX();

Page 75: Laborator geometrie computationala

13

dragY=e.getY(); repaint(50); } else dragS=false; if(dragW) { int X0=((Integer)(x2.elementAt(0))).intValue(); int X3=((Integer)(x2.elementAt(3))).intValue(); int Y3=((Integer)(y2.elementAt(3))).intValue(); int Y0=((Integer)(y2.elementAt(0))).intValue(); x2.setElementAt(X3+e.getX()-dragX,3); //x2.setElementAt(x1.elementAt(1),1); x2.setElementAt(X0+e.getX()-dragX,0); //x2.setElementAt(x1.elementAt(3),3); //y2.setElementAt(Y3+e.getY()-dragY,3); //y2.setElementAt(y1.elementAt(1),1); //y2.setElementAt(Y3+e.getY()-dragY,1); //y2.setElementAt(y1.elementAt(3),3); x1.add(((Integer)(x2.elementAt(0))).intValue()); x1.add(((Integer)(x2.elementAt(1))).intValue()); x1.add(((Integer)(x2.elementAt(2))).intValue()); x1.add(((Integer)(x2.elementAt(3))).intValue()); y1.add(((Integer)(y2.elementAt(0))).intValue()); y1.add(((Integer)(y2.elementAt(1))).intValue()); y1.add(((Integer)(y2.elementAt(2))).intValue()); y1.add(((Integer)(y2.elementAt(3))).intValue()); dragX=e.getX(); dragY=e.getY(); repaint(50); } else dragW=false; if(dragE) { int X2=((Integer)(x2.elementAt(2))).intValue(); int X1=((Integer)(x2.elementAt(1))).intValue(); int Y2=((Integer)(y2.elementAt(2))).intValue(); int Y1=((Integer)(y2.elementAt(1))).intValue(); x2.setElementAt(X2+e.getX()-dragX,2); //x2.setElementAt(x1.elementAt(1),1); x2.setElementAt(X1+e.getX()-dragX,1); //x2.setElementAt(x1.elementAt(3),3);

Page 76: Laborator geometrie computationala

14

//y2.setElementAt(Y3+e.getY()-dragY,3); //y2.setElementAt(y1.elementAt(1),1); //y2.setElementAt(Y3+e.getY()-dragY,1); //y2.setElementAt(y1.elementAt(3),3); x1.add(((Integer)(x2.elementAt(0))).intValue()); x1.add(((Integer)(x2.elementAt(1))).intValue()); x1.add(((Integer)(x2.elementAt(2))).intValue()); x1.add(((Integer)(x2.elementAt(3))).intValue()); y1.add(((Integer)(y2.elementAt(0))).intValue()); y1.add(((Integer)(y2.elementAt(1))).intValue()); y1.add(((Integer)(y2.elementAt(2))).intValue()); y1.add(((Integer)(y2.elementAt(3))).intValue()); dragX=e.getX(); dragY=e.getY(); repaint(50); } else dragE=false; } } public void set(int nr) { if(nr==1) { normal=true; drag=false; setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); hand=false; } if(nr==2) { if(desenat) { hand=true; setCursor(new Cursor(Cursor.HAND_CURSOR)); normal=false; drag=false; resize=false; } else JOptionPane.showMessageDialog(null,"Trebuie mai intai sa aveti desenat un dreptunghi!","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } if(nr==3) { if(desenat) { resize=true; setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); hand=false; normal=false; }

Page 77: Laborator geometrie computationala

15

} } } Clasa ajutatoare. Creaza obiecte de tip Punct6 package prob6; import java.util.*; public class Punct6 implements Comparable{ private double x,y; public Punct6(double x,double y){ this.x=x; this.y=y; } public double getX(){ return x; } public double getY(){ return y; } public int compareTo(Object o){ if(x>((Punct6)o).getX()) return 255; if((x==((Punct6)o).getX())&&(y==((Punct6)o).getY())) return 0; if((x==((Punct6)o).getX())&&(y<((Punct6)o).getY())) return -256; if((x==((Punct6)o).getX())&&(y>((Punct6)o).getY())) return 256; return -256; } }

Page 78: Laborator geometrie computationala

1

Laborator 7

Se da N puncte fixe. Si apoi se introduc M Dreptunghiuri. Sa se numere cate puncte se afla in interiorul dreptunghiului (si care) prin Metoda Arborelui Bidimensional. package prob7; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class Problema7 extends JPanel{ private Desen7 canvas; private JButton b1,b2,b3; private JTextField fisier; private Set p,m; private AscultatorButon ab; public Problema7(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); canvas=new Desen7(); b1=new JButton("1"); b1.addActionListener(ab); b2=new JButton("2"); b2.addActionListener(ab); b3=new JButton("3"); b3.addActionListener(ab); fisier=new JTextField(10); fisier.setText("prob5.txt"); fisier.setVisible(false); JPanel p=new JPanel(); JPanel p3=new JPanel(new GridLayout(2,1)); JPanel p1=new JPanel(); p1.add(b1); p1.add(b2); p1.add(b3); JPanel p2=new JPanel();

Page 79: Laborator geometrie computationala

2

p2.add(fisier); p3.add(p1); p3.add(p2); p.add(p3); this.add(p,BorderLayout.WEST); this.add(canvas,BorderLayout.CENTER); }//Problema3() --> constructorul "clasei" public class AscultatorButon implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==b1) { canvas.set(1); } if(e.getSource()==b2) { canvas.set(2); } if(e.getSource()==b3) { canvas.set(3); } }//actionPerformed() } } Algoritmul pt. problema 7 package prob7; import java.util.*; public class Algoritm7{ private ArrayList al;//multimea punctelor private Punct7[] list1,list2; private Punct7 vf; private Vector v,S; public Algoritm7 (ArrayList al){ this.al=al; makeList1(); makeList2(); for(int i=0;i<list1.length;++i) { System.out.print(list1[i].getNr()+" "); } System.out.println(); for(int i=0;i<list2.length;++i) { System.out.print(list2[i].getNr()+" ");

Page 80: Laborator geometrie computationala

3

} makeBinaryTree(); } public void makeList1() { TreeSet list; Object[] aux=al.toArray(); list=new TreeSet(); for(int i=0;i<aux.length;++i) list.add(aux[i]); Iterator it=list.iterator(); list1=new Punct7[aux.length]; int i=0; while(it.hasNext()) { Punct7 pct=(Punct7)(it.next()); list1[i++]=pct; } } public void makeList2() { /*Object[] aux=al.toArray(); list2=new Punct7[aux.length]; for(int i=0;i<aux.length;++i) list2[i]=(Punct7)aux[i]; for(int i=0;i<list2.length;++i){ for(int j=0;j<list2.length;++j) if(list2[i].getY()<list2[j].getY()) { Punct7 aux1=list2[i]; list2[i]=list2[j]; list2[j]=aux1; } } //qSort(0,aux.length-1); */ list2=new Punct7[list1.length]; for(int i=0;i<list1.length;++i) list2[i]=list1[i]; for(int i=0;i<list2.length-1;++i) for(int j=i;j<list2.length;++j) if(list2[i].getY()>list2[j].getY()) { Punct7 auxP=list2[i]; list2[i]=list2[j]; list2[j]=auxP; } }

Page 81: Laborator geometrie computationala

4

public void qSort(int s,int d) { int i=s; int j=d; Punct7 m=list2[(i+j)/2]; Punct7 aux; do { while(list2[i].getY()<m.getY()) i++; while(list2[j].getY()>m.getY()) j--; if(i<=j) { aux=list2[i]; list2[i]=list2[j]; list2[j]=aux; i++; j--; } } while(i<=j); if(s<j) qSort(s,j); if(d>i) qSort(i,d); } public int cautareBinara(int s, int d, Punct7 punct) { int nr=(s+d)/2; if((list2[nr].getX()==punct.getX())&&(punct.getY()==list2[nr].getY())) return nr; if(s<d) { boolean ok=true; //if((list2[nr].getY()==punct.getY())&&(list2[nr].getX()>punct.getX())) ok=false; if(list2[nr].getY()<punct.getY()) ok=false; if(ok) return cautareBinara(s,nr-1,punct); else return cautareBinara(nr+1,d,punct); } return -256; } public void makeBinaryTree(){ v=new Vector(); boolean[] b=new boolean[list1.length]; list1[list1.length/2].setType(1002); for(int i=0;i<list1.length/2;++i) { //if (list1[i].getNr()==i) b[list1[i].getNr()]=true; } list1[list1.length/2].setS(ordonata(0,list1.length/2-1,0,list2.length-1,b)); b=new boolean[list1.length];

Page 82: Laborator geometrie computationala

5

for(int i=list1.length/2+1;i<list1.length;++i) { //if (list1[i].getNr()==i) b[list1[i].getNr()]=true; } list1[list1.length/2].setD(ordonata(list1.length/2+1,list1.length-1,0,list2.length-1,b)); vf=list1[list1.length/2]; v.add(list1[list1.length/2]); System.out.println("Varf "+vf.getNr()); if(vf.getD()!=null) System.out.println("Dreapta "+vf.getD().getNr()); if(vf.getS()!=null) System.out.println("Stanga "+vf.getS().getNr()); for(int i=0;i<v.size();++i) { Punct7 punct=(Punct7)(v.elementAt(i)); System.out.print(i+" "+punct.getNr()); if(punct.getD()!=null) System.out.print(" Dreapta "+punct.getD().getNr()); if(punct.getS()!=null) System.out.println(" Stanga "+punct.getS().getNr()); } } public Punct7 ordonata(int xa, int ya, int xo, int yo, boolean[] bool) { Vector aux=new Vector(); for (int i=0;i<list2.length;++i) { if (bool[list2[i].getNr()]) aux.add(list2[i]); } if(aux.size()==0) return null; Punct7 pct=(Punct7)(aux.elementAt(aux.size()/2)); pct.setType(1001); int nrr=pct.getNr(); System.out.print("ord: "+nrr+" "+aux.size()+" : "); for(int i=0;i<aux.size();i++) System.out.print(((Punct7)(aux.elementAt(i))).getNr()+" "); System.out.println(); int t=0; for (int i=0;i<list2.length;++i) if(list2[i].getNr()==nrr) { t=i; break; } boolean[] b=new boolean[list2.length]; for(int i=0;i<aux.size()/2;++i) b[((Punct7)(aux.elementAt(i))).getNr()]=true;

Page 83: Laborator geometrie computationala

6

pct.setS(abscisa(xa,ya,xo,t-1,b)); b=new boolean[list2.length]; for(int i=aux.size()/2+1;i<aux.size();++i) b[((Punct7)(aux.elementAt(i))).getNr()]=true; pct.setD(abscisa(xa,ya,t+1,yo,b)); v.add(pct); return pct; } public Punct7 abscisa(int xa, int ya, int xo, int yo, boolean[] bool) { Vector aux=new Vector(); for (int i=0;i<list1.length;++i) { if (bool[list1[i].getNr()]) aux.add(list1[i]); } if(aux.size()==0) return null; System.out.println(aux.size()); Punct7 pct=(Punct7)(aux.elementAt(aux.size()/2)); pct.setType(1002); v.add(pct); int nrr=pct.getNr(); System.out.print("abs: "+nrr+" "+aux.size()+" : "); for(int i=0;i<aux.size();i++) System.out.print(((Punct7)(aux.elementAt(i))).getNr()+" "); System.out.println(); int t=-5; for (int i=0;i<list1.length;++i) if(list1[i].getNr()==nrr) { t=i; break; } boolean[] b=new boolean[list1.length]; for(int i=0;i<aux.size()/2;++i) b[((Punct7)(aux.elementAt(i))).getNr()]=true; pct.setS(ordonata(xa,t-1,xo,yo,b)); b=new boolean[list1.length]; for(int i=aux.size()/2+1;i<aux.size();++i) b[((Punct7)(aux.elementAt(i))).getNr()]=true; pct.setD(ordonata(t+1,ya,xo,yo,b)); v.add(pct); return pct;

Page 84: Laborator geometrie computationala

7

} public Vector cauta(double x1,double y1, double x2, double y2) { S=new Vector(); System.out.println("Punctele din plan sunt:"+v.size()); /*for(int i=0;i<list1.length;++i) { Punct7 punct=(Punct7)(list1[i]); System.out.println(i+" "+punct.getNr()+" "+punct.getX()+" "+punct.getY()); //if(punct.getD()!=null) System.out.print(" Dreapta "+punct.getD().getNr()); //if(punct.getS()!=null) System.out.println(" Stanga "+punct.getS().getNr()); } */ for(int i=0;i<v.size();i++) { Punct7 punct=(Punct7)(v.elementAt(i)); System.out.println(i+" "+punct.getNr()+" "+punct.getX()+" "+punct.getY()); if(punct.getD()!=null) System.out.print(" Dreapta "+punct.getD().getNr()); if(punct.getS()!=null) System.out.print(" Stanga "+punct.getS().getNr()); System.out.println(); } System.out.println("Punctele dreptunghiului sunt:"); System.out.println(x1+" "+y1); System.out.println(x2+" "+y2); c(vf,x1,y1,x2,y2); return S; } public void c(Punct7 pct, double x1,double y1, double x2, double y2) { double l,r; double m; if(pct.getType()==Punct7.V) { l=x1; r=x2; m=pct.getX(); } else { l=y2;

Page 85: Laborator geometrie computationala

8

r=y1; m=pct.getY(); } if (pct.getX() >= x1 && pct.getX() <= x2 && pct.getY() <= y1 && pct.getY() >= y2){ System.out.println("S-a adaugat un punct! "+pct.getNr()); S.add(pct); //if (pct.getS()!=null) c(pct.getS(), x1,y1,x2,y2); //if (pct.getD()!=null) c(pct.getD(), x1,y1,x2,y2); //else{ //if () } if (m>=l) { System.out.println("Este spre stanga"); if (pct.getS()!=null) c(pct.getS(), x1,y1,x2,y2); } if (m<=r) { System.out.println("Este spre stanga"); if (pct.getD()!=null) c(pct.getD(), x1,y1,x2,y2); } //} } } Suprafata de desen pentru problema 7 package prob7; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Graphics.*; import java.util.*; public class Desen7 extends Canvas { private boolean mouseIntroducereDate=true; private ArrayList al; private Algoritm7 a; private double mx,my,min,max; private int contor=0,nr=0,dragX,dragY,c=0; private Vector x,y,x1,y1,x2,y2; private boolean hand=false, normal=true,desenat=false,drag=false, resize=false, dragN=false, dragS=false, dragW=false, dragE=false; public Desen7(){

Page 86: Laborator geometrie computationala

9

al=new ArrayList(); this.enableEvents(MouseEvent.MOUSE_CLICKED); this.enableEvents(MouseEvent.MOUSE_DRAGGED); this.enableEvents(MouseEvent.MOUSE_RELEASED); this.enableEvents(MouseEvent.MOUSE_MOVED); this.enableEvents(506); //MouseEvent.MOUSE_PRESSED setSize(500,500); x=new Vector(); y=new Vector(); x1=new Vector(); y1=new Vector(); //setCursor(new Cursor(Cursor.HAND_CURSOR)); } public void paint(Graphics g) { g.setColor(Color.BLACK); g.drawLine(0,0,500,0); g.drawLine(0,0,0,500); g.drawLine(500,0,500,500); g.drawLine(0,500,500,500); //trasez axa x' g.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g.drawString("x",calcX(21),calcY(-1)); g.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y' g.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20)); g.drawString("y",calcX(-1),calcY(21)); g.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); Object[] puncte=al.toArray(); for(int i=0;i<puncte.length;++i) { Punct7 punct=(Punct7)puncte[i]; g.fillOval(calcX(punct.getX())-2, calcY(punct.getY())-2,4,4); g.drawString("P"+punct.getNr(),calcX(punct.getX())+1,calcY(punct.getY())+1); } if(x1.size()==1) { g.setColor(Color.red);

Page 87: Laborator geometrie computationala

10

g.fillOval(((Integer) (x1.elementAt(0))).intValue()-2,((Integer) (y1.elementAt(0))).intValue()-2,4,4); g.setColor(Color.black); } if(x1.size()==2) { g.setColor(Color.red); g.fillOval(((Integer) (x1.elementAt(0))).intValue()-2,((Integer) (y1.elementAt(0))).intValue()-2,4,4); g.fillOval(((Integer) (x1.elementAt(1))).intValue()-2,((Integer) (y1.elementAt(1))).intValue()-2,4,4); g.setColor(Color.black); } if(x1.size()==4){ g.drawLine( ((Integer) (x1.elementAt(0))).intValue(), ((Integer) (y1.elementAt(0))).intValue(), ((Integer) (x1.elementAt(2))).intValue(), ((Integer) (y1.elementAt(2))).intValue()); g.drawLine( ((Integer) (x1.elementAt(1))).intValue(), ((Integer) (y1.elementAt(1))).intValue(), ((Integer) (x1.elementAt(3))).intValue(), ((Integer) (y1.elementAt(3))).intValue()); g.drawLine( ((Integer) (x1.elementAt(0))).intValue(), ((Integer) (y1.elementAt(0))).intValue(), ((Integer) (x1.elementAt(3))).intValue(), ((Integer) (y1.elementAt(3))).intValue()); g.drawLine( ((Integer) (x1.elementAt(2))).intValue(), ((Integer) (y1.elementAt(2))).intValue(), ((Integer) (x1.elementAt(1))).intValue(), ((Integer) (y1.elementAt(1))).intValue()); g.setColor(Color.RED); g.fillOval(((Integer) (x1.elementAt(0))).intValue()-2,((Integer) (y1.elementAt(0))).intValue()-2,4,4); g.fillOval(((Integer) (x1.elementAt(1))).intValue()-2,((Integer) (y1.elementAt(1))).intValue()-2,4,4); g.fillOval(((Integer) (x1.elementAt(2))).intValue()-2,((Integer) (y1.elementAt(2))).intValue()-2,4,4); g.fillOval(((Integer) (x1.elementAt(3))).intValue()-2,((Integer) (y1.elementAt(3))).intValue()-2,4,4); //System.out.println(b.size()); //if(nr==1) g.drawString("Este un punct",240,440); double xx1=(((Integer) (x1.elementAt(0))).intValue()-250)/10.0; double xx2=(((Integer) (x1.elementAt(1))).intValue()-250)/10.0; double yy1=(250-((Integer) (y1.elementAt(0))).intValue())/10.0; double yy2=(250-((Integer) (y1.elementAt(1))).intValue())/10.0; Vector auxiliar=a.cauta(xx1, yy1, xx2, yy2); //mx=(mouseX-250)/10.0; //my=(250-mouseY)/10.0; g.drawString(auxiliar.size()+"",10,10);

Page 88: Laborator geometrie computationala

11

g.setColor(Color.blue); for(int i=0;i<auxiliar.size();++i) { g.fillOval(calcX(((Punct7)(auxiliar.elementAt(i))).getX())-2,calcY(((Punct7)(auxiliar.elementAt(i))).getY())-2,4,4); } g.setColor(Color.red); nr=0; desenat=true; contor=0; x2=x1; y2=y1; x1=new Vector(); y1=new Vector(); //mousePrim=false; } } public int calcX(double x){ return (int)(250+x*10); }//calcX(int x) public int calcY(double y){ return (int)(250-y*10); }//calcY(int y) public void processMouseEvent(MouseEvent e) { if((normal)&&(e.getID()==MouseEvent.MOUSE_CLICKED)){ if((mouseIntroducereDate)&&(e.getButton()==MouseEvent.BUTTON3)) { mouseIntroducereDate=false; a=new Algoritm7(al); } if((mouseIntroducereDate)&&(e.getButton()==MouseEvent.BUTTON1)) { al.add(new Punct7((e.getX()-250)/10.0,(250-e.getY())/10.0,c++)); repaint(); } if((!mouseIntroducereDate)&&(e.getButton()==MouseEvent.BUTTON1)) { contor++; int mouseX=e.getX(); int mouseY=e.getY(); mx=(mouseX-250)/10.0; my=(250-mouseY)/10.0; contor=x1.size(); System.out.println(contor);

Page 89: Laborator geometrie computationala

12

x1.add(new Integer(mouseX)); y1.add(new Integer(mouseY)); if(x1.size()==2) { mx=Math.max(((Integer)(x1.elementAt(0))).intValue(),((Integer)(x1.elementAt(1))).intValue()); my=Math.min(((Integer)(y1.elementAt(0))).intValue(),((Integer)(y1.elementAt(1))).intValue()); x1.add(new Integer((int)mx)); y1.add(new Integer((int)my)); mx=Math.min(((Integer)(x1.elementAt(0))).intValue(),((Integer)(x1.elementAt(1))).intValue()); my=Math.max(((Integer)(y1.elementAt(0))).intValue(),((Integer)(y1.elementAt(1))).intValue()); x1.add(new Integer((int)mx)); y1.add(new Integer((int)my)); if(((Integer)(x1.elementAt(0))).intValue()> ((Integer)(x1.elementAt(1))).intValue() ) { Object o=x1.elementAt(0); x1.setElementAt(x1.elementAt(1),0); x1.setElementAt(o,1); o=y1.elementAt(0); y1.setElementAt(y1.elementAt(1),0); y1.setElementAt(o,1); //b=new boolean[x.size()]; } } System.out.println("aci"); repaint(); } } } public void processMouseMotionEvent(MouseEvent e){ if((hand)&&(e.getID()==MouseEvent.MOUSE_DRAGGED)&&(!drag)){ System.out.println("mous pointat "+e.getX()+" "+e.getY()); boolean ok=false; if((((Integer)(x2.elementAt(0))).intValue()<e.getX())&& (((Integer)(y2.elementAt(0))).intValue()<e.getY())&& (((Integer)(x2.elementAt(1))).intValue()>e.getX())&& (((Integer)(y2.elementAt(1))).intValue()>e.getY())&& (((Integer)(x2.elementAt(2))).intValue()>e.getX())&& (((Integer)(y2.elementAt(2))).intValue()<e.getY())&&

Page 90: Laborator geometrie computationala

13

(((Integer)(x2.elementAt(3))).intValue()<e.getX())&& (((Integer)(y2.elementAt(3))).intValue()>e.getY())) { drag=true; dragX=e.getX(); dragY=e.getY(); } } if((drag)&&(e.getID()==MouseEvent.MOUSE_DRAGGED)){ System.out.println("mous tras "+e.getX()+" "+e.getY()); x1.add(new Integer(((Integer)(x2.elementAt(0))).intValue()+e.getX()-dragX)); x1.add(new Integer(((Integer)(x2.elementAt(1))).intValue()+e.getX()-dragX)); x1.add(new Integer(((Integer)(x2.elementAt(2))).intValue()+e.getX()-dragX)); x1.add(new Integer(((Integer)(x2.elementAt(3))).intValue()+e.getX()-dragX)); y1.add(new Integer(((Integer)(y2.elementAt(0))).intValue()+e.getY()-dragY)); y1.add(new Integer(((Integer)(y2.elementAt(1))).intValue()+e.getY()-dragY)); y1.add(new Integer(((Integer)(y2.elementAt(2))).intValue()+e.getY()-dragY)); y1.add(new Integer(((Integer)(y2.elementAt(3))).intValue()+e.getY()-dragY)); x2.setElementAt(x1.elementAt(0),0); x2.setElementAt(x1.elementAt(1),1); x2.setElementAt(x1.elementAt(2),2); x2.setElementAt(x1.elementAt(3),3); y2.setElementAt(y1.elementAt(0),0); y2.setElementAt(y1.elementAt(1),1); y2.setElementAt(y1.elementAt(2),2); y2.setElementAt(y1.elementAt(3),3); dragX=e.getX(); dragY=e.getY(); repaint(500); } else drag=false; if((resize)&&(e.getID()==MouseEvent.MOUSE_MOVED)) { //System.out.println("mous mutat "+e.getX()+" "+e.getY()); int X0=((Integer)(x2.elementAt(0))).intValue(); int X1=((Integer)(x2.elementAt(1))).intValue(); int X2=((Integer)(x2.elementAt(2))).intValue(); int X3=((Integer)(x2.elementAt(3))).intValue(); int Y0=((Integer)(y2.elementAt(0))).intValue();

Page 91: Laborator geometrie computationala

14

int Y1=((Integer)(y2.elementAt(1))).intValue(); int Y2=((Integer)(y2.elementAt(2))).intValue(); int Y3=((Integer)(y2.elementAt(3))).intValue(); if((e.getX()>X0)&&(Math.abs(e.getY()-Y0)<5)&& (e.getX()<X2)&&(Math.abs(e.getY()-Y2)<5)) { setCursor(new Cursor(Cursor.N_RESIZE_CURSOR)); dragX=e.getX(); dragY=e.getY(); dragN=true; } else { dragN=false; } if((e.getX()>X3)&&(Math.abs(e.getY()-Y3)<5)&& (e.getX()<X1)&&(Math.abs(e.getY()-Y1)<5)) { setCursor(new Cursor(Cursor.S_RESIZE_CURSOR)); dragX=e.getX(); dragY=e.getY(); dragS=true; } else { dragS=false; } if((e.getY()>Y0)&&(Math.abs(e.getX()-X0)<5)&& (e.getY()<Y3)&&(Math.abs(e.getX()-X3)<5)) { setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); dragX=e.getX(); dragY=e.getY(); dragW=true; } else { dragW=false; } if((e.getY()>Y2)&&(Math.abs(e.getX()-X2)<5)&& (e.getY()<Y1)&&(Math.abs(e.getX()-X1)<5)) { setCursor(new Cursor(Cursor.E_RESIZE_CURSOR)); dragX=e.getX(); dragY=e.getY(); dragE=true; } else { dragE=false; } if((!dragN)&&(!dragS)&&(!dragW)&&(!dragE)) setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } if((resize)&&(e.getID()==MouseEvent.MOUSE_DRAGGED)) { if(dragN) { int X0=((Integer)(x2.elementAt(0))).intValue();

Page 92: Laborator geometrie computationala

15

int X2=((Integer)(x2.elementAt(2))).intValue(); int Y0=((Integer)(y2.elementAt(0))).intValue(); int Y2=((Integer)(y2.elementAt(2))).intValue(); //x2.setElementAt(X0+e.getX()-dragX,0); //x2.setElementAt(x1.elementAt(1),1); //x2.setElementAt(X2+e.getX()-dragX,2); //x2.setElementAt(x1.elementAt(3),3); y2.setElementAt(Y0+e.getY()-dragY,0); //y2.setElementAt(y1.elementAt(1),1); y2.setElementAt(Y2+e.getY()-dragY,2); //y2.setElementAt(y1.elementAt(3),3); x1.add(new Integer(((Integer)(x2.elementAt(0))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(1))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(2))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(3))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(0))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(1))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(2))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(3))).intValue())); dragX=e.getX(); dragY=e.getY(); repaint(50); } else dragN=false; if(dragS) { int X3=((Integer)(x2.elementAt(3))).intValue(); int X1=((Integer)(x2.elementAt(1))).intValue(); int Y3=((Integer)(y2.elementAt(3))).intValue(); int Y1=((Integer)(y2.elementAt(1))).intValue(); //x2.setElementAt(X3+e.getX()-dragX,3); //x2.setElementAt(x1.elementAt(1),1); //x2.setElementAt(X1+e.getX()-dragX,1); //x2.setElementAt(x1.elementAt(3),3);

Page 93: Laborator geometrie computationala

16

y2.setElementAt(Y3+e.getY()-dragY,3); //y2.setElementAt(y1.elementAt(1),1); y2.setElementAt(Y3+e.getY()-dragY,1); //y2.setElementAt(y1.elementAt(3),3); x1.add(new Integer(((Integer)(x2.elementAt(0))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(1))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(2))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(3))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(0))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(1))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(2))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(3))).intValue())); dragX=e.getX(); dragY=e.getY(); repaint(50); } else dragS=false; if(dragW) { int X0=((Integer)(x2.elementAt(0))).intValue(); int X3=((Integer)(x2.elementAt(3))).intValue(); int Y3=((Integer)(y2.elementAt(3))).intValue(); int Y0=((Integer)(y2.elementAt(0))).intValue(); x2.setElementAt(X3+e.getX()-dragX,3); //x2.setElementAt(x1.elementAt(1),1); x2.setElementAt(X0+e.getX()-dragX,0); //x2.setElementAt(x1.elementAt(3),3); //y2.setElementAt(Y3+e.getY()-dragY,3); //y2.setElementAt(y1.elementAt(1),1); //y2.setElementAt(Y3+e.getY()-dragY,1); //y2.setElementAt(y1.elementAt(3),3); x1.add(new Integer(((Integer)(x2.elementAt(0))).intValue()));

Page 94: Laborator geometrie computationala

17

x1.add(new Integer(((Integer)(x2.elementAt(1))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(2))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(3))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(0))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(1))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(2))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(3))).intValue())); dragX=e.getX(); dragY=e.getY(); repaint(50); } else dragW=false; if(dragE) { int X2=((Integer)(x2.elementAt(2))).intValue(); int X1=((Integer)(x2.elementAt(1))).intValue(); int Y2=((Integer)(y2.elementAt(2))).intValue(); int Y1=((Integer)(y2.elementAt(1))).intValue(); x2.setElementAt(X2+e.getX()-dragX,2); //x2.setElementAt(x1.elementAt(1),1); x2.setElementAt(X1+e.getX()-dragX,1); //x2.setElementAt(x1.elementAt(3),3); //y2.setElementAt(Y3+e.getY()-dragY,3); //y2.setElementAt(y1.elementAt(1),1); //y2.setElementAt(Y3+e.getY()-dragY,1); //y2.setElementAt(y1.elementAt(3),3); x1.add(new Integer(((Integer)(x2.elementAt(0))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(1))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(2))).intValue())); x1.add(new Integer(((Integer)(x2.elementAt(3))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(0))).intValue()));

Page 95: Laborator geometrie computationala

18

y1.add(new Integer(((Integer)(y2.elementAt(1))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(2))).intValue())); y1.add(new Integer(((Integer)(y2.elementAt(3))).intValue())); dragX=e.getX(); dragY=e.getY(); repaint(50); } else dragE=false; } } public void set(int nr) { if(nr==1) { normal=true; drag=false; setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); hand=false; } if(nr==2) { if(desenat) { hand=true; setCursor(new Cursor(Cursor.HAND_CURSOR)); normal=false; drag=false; resize=false; } else JOptionPane.showMessageDialog(null,"Trebuie mai intai sa aveti desenat un dreptunghi!","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); } if(nr==3) { if(desenat) { resize=true; setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); hand=false; normal=false; } } } }

Page 96: Laborator geometrie computationala

19

Clasa ajutatoare, Creaza obiecte de tip Punct7 package prob7; import java.util.*; public class Punct7 implements Comparable{ private double x,y; private int nr,tip; public final static int O=1001, V=1002; private Punct7 d,s; public Punct7(double x,double y,int nr){ this.x=x; this.y=y; this.nr=nr; } public double getX(){ return x; } public double getY(){ return y; } public void setY(double y) { this.y=y; } public int getNr() { return nr; } public int compareTo(Object o){ if(x>((Punct7)o).getX()) return 255; if((x==((Punct7)o).getX())&&(y==((Punct7)o).getY())) return 0; if((x==((Punct7)o).getX())&&(y<((Punct7)o).getY())) return -256; if((x==((Punct7)o).getX())&&(y>((Punct7)o).getY())) return 256; return -256; } public void setType(int nr) { tip=nr; }

Page 97: Laborator geometrie computationala

20

public int getType() { return tip; } public void setD(Punct7 d) { this.d=d; } public void setS(Punct7 s) { this.s=s; } public Punct7 getD() { return d; } public Punct7 getS() { return s; } }

Page 98: Laborator geometrie computationala

1

Laborator 8

Algoritmul Graham's Scan pt. aflarea infasuratorii convexe. package prob8; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Problema8 extends JPanel{ private Desen8 canvas; private JButton infasoara, play; private AscultatorButon ab; public Problema8(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); JPanel panel=new JPanel (new GridLayout(2,1)); infasoara=new JButton("Infasoara"); infasoara.addActionListener(ab); panel.add(infasoara); play=new JButton("Play"); play.addActionListener(ab); panel.add(play); play.setVisible(false); canvas=new Desen8(); JPanel panel1=new JPanel(); panel1.add(panel); this.add(panel1,BorderLayout.WEST); //JPanel ppp=new JPanel(); //ppp.add(canvas); this.add(canvas,BorderLayout.CENTER); } public class AscultatorButon implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==infasoara){

Page 99: Laborator geometrie computationala

2

canvas.setStare(1); System.out.println("AM apasat!"); infasoara.setEnabled(false); } else if(e.getSource()==play){ //canvas.set(1); } } } } Algoritmul pt. problema 8 package prob8; import java.awt.*; import java.util.*; public class Algoritm8{ private ArrayList puncte; private Object[] obiecte; private Punct8 m,mm; private int mTx,mTy; private boolean vizitat=false; public Algoritm8(ArrayList p){ puncte=new ArrayList(p); } private void fixM(){ double x1,y1,x2,y2,x3,y3; Object [] aux=puncte.toArray(); //minx=Integer.MAX_VALUE; //miny=Integer.MAX_VALUE; //maxy=Integer.MIN_VALUE; Punct8 minx=new Punct8(Integer.MAX_VALUE,0); Punct8 miny=new Punct8(0,Integer.MAX_VALUE); Punct8 maxy=new Punct8(0,Integer.MIN_VALUE); for(int i=0;i<aux.length;++i){ Punct8 p=(Punct8)aux[i]; if(minx.getX()>p.getX()) minx=p; if(miny.getY()>p.getY()) miny=p; if(maxy.getY()<p.getY()) maxy=p; }

Page 100: Laborator geometrie computationala

3

x1=minx.getX(); x2=miny.getX(); x3=maxy.getX(); y1=minx.getY(); y2=miny.getY(); y3=maxy.getY(); m=new Punct8((int)((x1+x2+x3)/3),(int)((y1+y2+y3)/3)); mm=new Punct8((int)((x1+x2+x3)/3),(int)((y1+y2+y3)/3)); } public void translatare(){ this.fixM(); System.out.println("M"); System.out.println(m.getX()+" "+m.getY()); Iterator it=puncte.iterator(); ArrayList puncte1=new ArrayList(); while(it.hasNext()){ Punct8 pp=(Punct8) it.next(); Punct8 p=new Punct8( pp.getX(),pp.getY() ); mTx=250-(int)m.getX();//se calculeaza cu ca sa se faca transalatarea mTx pt.x mTy=250-(int)m.getY();//si mTy pt. y p.translatare(mTx,mTy); puncte1.add(p); } puncte=puncte1; it=puncte.iterator(); while(it.hasNext()){ Punct8 p=(Punct8) it.next(); System.out.println(p.getX()+" - "+p.getY()); } m.setX(0); m.setY(0); } public void sorteaza(){ this.translatare(); obiecte=puncte.toArray(); //for(int i=0;i<obiecte.length; i++) System.out.println(((Punct8) obiecte[i]).getX()+" "+((Punct8) obiecte[i]).getY()); //System.out.println("\n"); /*int min=1000; int j=1; for(int i=0;i<obiecte.length;i++) if( min > ((Punct8) obiecte[i]).getY() ) {

Page 101: Laborator geometrie computationala

4

min=((Punct8) obiecte[i]).getY(); j=i; } Object o=obiecte[0]; obiecte[0]=obiecte[j]; obiecte[j]=o; for(int i=0;i<obiecte.length; i++) System.out.println(((Punct8) obiecte[i]).getX()+" "+((Punct8) obiecte[i]).getY()); System.out.println("\n \n \n"); boolean b=false; do{ b=true; for(int i=1; i<obiecte.length; i++){ if( ((Punct8) obiecte[i-1]).compareTo(obiecte[i])<0 ){ Object aux=obiecte[i-1]; obiecte[i-1]=obiecte[i]; obiecte[i]=aux; b=false; } } }while(b==false);*/ Arrays.sort(obiecte); System.out.println("Dupa sortare"); for(int i=0;i<obiecte.length; i++) System.out.println((((Punct8) obiecte[i]).getX()-mTx)+" "+(((Punct8) obiecte[i]).getY()-mTy)); } public void infasor(){ sorteaza(); int l=0,j=0; double det=0d; int i=0; int maxy=Integer.MIN_VALUE; int max=0; //pun pe prima pozitie punctul cel mai de jos for(i=0;i<obiecte.length;++i) { if(maxy<((Punct8)obiecte[i]).getY()) { maxy=((Punct8)obiecte[i]).getY(); max=i; } }

Page 102: Laborator geometrie computationala

5

Object[] aux=new Object[obiecte.length]; int ll=0; for(i=max;i<obiecte.length;++i) aux[ll++]=obiecte[i]; for(i=0;i<max;++i) aux[ll++]=obiecte[i]; obiecte=aux; i=0; do{ if(i==obiecte.length) i=0; if(i<=obiecte.length-3) {det=calcDet(((Punct8)obiecte[i]).getX(),((Punct8)obiecte[i]).getY(),((Punct8)obiecte[i+1]).getX(),((Punct8)obiecte[i+1]).getY(),((Punct8)obiecte[i+2]).getX(),((Punct8)obiecte[i+2]).getY()); j=i+1; } else if(i==obiecte.length-2) {det=calcDet(((Punct8)obiecte[i]).getX(),((Punct8)obiecte[i]).getY(),((Punct8)obiecte[i+1]).getX(),((Punct8)obiecte[i+1]).getY(),((Punct8)obiecte[0]).getX(),((Punct8)obiecte[0]).getY()); j=i+1; l=1;vizitat=true; } else if(i==obiecte.length-1) {det=calcDet(((Punct8)obiecte[i]).getX(),((Punct8)obiecte[i]).getY(),((Punct8)obiecte[0]).getX(),((Punct8)obiecte[0]).getY(),((Punct8)obiecte[1]).getX(),((Punct8)obiecte[1]).getY()); j=0; l=2; }//am modificat j=1 cu j=0 //if((l==2)&&(vizitat)) break; //in caz caci ultimul este la mijloc inseamna ca s-a terminat , dupa ce a fost ultimul :P if(det<=0){ //am modificat det<0 cu det > 0 //ArrayList oo=new ArrayList(); //for(int k=0;k<obiecte.length;k++) oo.add(obiecte[k]);//in loc de 1 am pus 0 //oo.remove(obiecte[j]);//nu se cunoaste fenomenul de egalitate :D Object[] obAux=new Object[obiecte.length-1]; for(int z=0;z<j;++z) obAux[z]=obiecte[z]; for(int z=j+1;z<obiecte.length;++z) obAux[z-1]=obiecte[z]; //oo.remove(j) obiecte=new Object[obAux.length]; for(int iii=0;iii<obiecte.length;++iii) obiecte[iii]=obAux[iii]; //obiecte=obAux; i=i-1;//am modificat i=i-1 cu i=0 ciudat dat numai asa merge, cred ca ar trebuie i=i-2 dar eu las asa if(i==-1) i=0; } else i++;//am scris else i++ }while(l!=2&&!vizitat); i=obiecte.length-2; if(i>=0)

Page 103: Laborator geometrie computationala

6

if(calcDet(((Punct8)obiecte[i]).getX(),((Punct8)obiecte[i]).getY(),((Punct8)obiecte[i+1]).getX(),((Punct8)obiecte[i+1]).getY(),((Punct8)obiecte[0]).getX(),((Punct8)obiecte[0]).getY())<=0) { Object[] obAux=new Object[obiecte.length-1]; for(int k=0;k<obAux.length;++k) obAux[k]=obiecte[k]; obiecte=new Object[obAux.length]; for(int k=0;k<obAux.length;++k) obiecte[k]=obAux[k]; } } public Object[] frontiera(){ for(int i=0;i<obiecte.length;i++) ((Punct8)obiecte[i]).translatare(-mTx,-mTy); return obiecte; } public double calcDet(double x1, double y1, double x2, double y2, double x3, double y3){ x1=(x1+mTx-250)/10.0; x2=(x2+mTx-250)/10.0; x3=(x3+mTx-250)/10.0; y1=(250-y1+mTy)/10.0; y2=(250-y2+mTy)/10.0; y3=(250-y3+mTy)/10.0; //return x1*-y2+x2*-y3+x3*-y1-x3*-y2-x1*-y3-x2*-y1; return x1*y2+x2*y3+x3*y1-x3*y2-x1*y3-x2*y1; } public Punct8 getM() { return mm; } } Suprafata de desen pentru problema 8 package prob8; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Graphics.*; import java.util.*; public class Desen8 extends Canvas{

Page 104: Laborator geometrie computationala

7

private ArrayList puncte,frontiera; private Object[] obiecte; private Vector x,y; private boolean potIntroducePuncte=true; private int stare; public Desen8(){ puncte=new ArrayList(); frontiera=new ArrayList(); x=new Vector(); y=new Vector(); stare=0; this.setSize(500,500); this.enableEvents(MouseEvent.MOUSE_CLICKED); } public void paint(Graphics g){ g.setColor(Color.BLACK); //chenarul g.drawLine(0,0,500,0); g.drawLine(0,0,0,500); g.drawLine(500,0,500,500); g.drawLine(0,500,500,500); //trasez axa x' g.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g.drawString("x",calcX(21),calcY(-1)); g.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y' g.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20)); g.drawString("y",calcX(-1),calcY(21)); g.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); //desenez punctele Iterator it=puncte.iterator(); while(it.hasNext()){ Punct8 punct=(Punct8) it.next(); g.fillOval((int)punct.getX()-2, (int)punct.getY()-2,4,4); }

Page 105: Laborator geometrie computationala

8

//daca am apasat butonul "Infasoara" (deci starea canvasului este 1) if( (puncte.size()>=3) && (stare==1) ){ it=puncte.iterator(); while(it.hasNext()){ Punct8 p=(Punct8) it.next(); System.out.println(p.getX()+" - "+p.getY()); } Algoritm8 a=new Algoritm8(puncte); //a.sorteaza(); a.infasor(); obiecte=a.frontiera(); if(obiecte.length>2) { int i; for(i=1;i<obiecte.length;i++) { g.drawLine( ((Punct8)obiecte[i-1]).getX(),((Punct8)obiecte[i-1]).getY(), ((Punct8)obiecte[i]).getX(), ((Punct8)obiecte[i]).getY() ); g.drawString(""+(i-1),((Punct8)obiecte[i-1]).getX()+1,((Punct8)obiecte[i-1]).getY()+1); System.out.println(((Punct8)obiecte[i-1]).getX()+" !!! "+((Punct8)obiecte[i-1]).getY()); } g.drawLine( ((Punct8)obiecte[i-1]).getX(),((Punct8)obiecte[i-1]).getY(), ((Punct8)obiecte[0]).getX(), ((Punct8)obiecte[0]).getY() ); g.drawString(""+(i-1),((Punct8)obiecte[i-1]).getX()+1,((Punct8)obiecte[i-1]).getY()+1); System.out.println(((Punct8)obiecte[i-1]).getX()+" !!! "+((Punct8)obiecte[i-1]).getY()); //Punct8 M=a.getM(); //g.drawString("M",((Punct8)obiecte[i-1]).getX()+1,((Punct8)obiecte[i-1]).getY()+1); //g.fillOval((int)M.getX()-2, (int)M.getY()-2,4,4); //am modificat stare=3 cu stare=1 pt. a nu disparea conturul acoperirii convexe } else if (stare==1) JOptionPane.showMessageDialog(null,"Desenul infasuratorii convexe nu este decat o linie dreapta si nu o voi desena :P !","Mesaj de eroare",JOptionPane.WARNING_MESSAGE); stare=2; System.out.println("Gata"); } }

Page 106: Laborator geometrie computationala

9

public int calcX(double x){ return (int)(250+x*10); }//calcX(int x) public int calcY(double y){ return (int)(250-y*10); }//calcY(int y) public void setStare(int tip){ stare=tip; } public void processMouseEvent(MouseEvent e){ //daca apas pe mouse si nu am apasat inca butonul "Infasoara" (starea canvasului este 0) if( (e.getID()==MouseEvent.MOUSE_CLICKED) && (stare==0) ){ Punct8 punct=new Punct8(e.getX(),e.getY()); puncte.add(punct); repaint(); } repaint(); } } Clasa ajutatoare. Creaza obiecte de tip Punct8 package prob8; import java.util.*; public class Punct8 implements Comparable{ private int x,y; public Punct8(int x, int y){ this.x=x; this.y=y; } public int getX(){ return x; } public int getY(){

Page 107: Laborator geometrie computationala

10

return y; } public void setX(int x){ this.x=x; } public void setY(int y){ this.y=y; } public void translatare(int xm, int ym){ x=x+xm; y=y+ym; } public int getCadran(){ //sunt schimbari in loc de 0 este 250 si am pus cadranele corect(ca numere) if( (x>250) && (y>=250) ) return 4; else if ( (x>=250) && (y<250) ) return 1; else if ( (x<=250) && (y>250) ) return 3 ; else if ( (x<250) && (y<=250) ) return 2 ; System.out.println("Err"); return 0; } public int compareTo(Object o){ if( ((Punct8) o).getCadran() < this.getCadran() ) return +256; //am corectat semnul de comparatie de la > in < else if ( (((Punct8) o).getCadran() == this.getCadran()) && getDet(x,y, ((Punct8)o).getX(), ((Punct8)o).getY()) <0 ) return +256; return -256; } public int getDet(int x, int y, int x1, int y1){ //return (x*y1-x1*y); //schimbat cu: return x*250+250*y1+x1*y-250*x1-y1*x-y*250; } }

Page 108: Laborator geometrie computationala

1

Laborator 9

Algoritmul Jarvis varianta AKL pt. aflarea infasuratorii convexe. package prob9; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Problema9 extends JPanel{ private Desen9 canvas; private JButton infasoara, play; private AscultatorButon ab; public Problema9(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); JPanel panel=new JPanel (new GridLayout(2,1)); infasoara=new JButton("Infasoara"); infasoara.addActionListener(ab); panel.add(infasoara); play=new JButton("Play"); play.addActionListener(ab); panel.add(play); play.setVisible(false); canvas=new Desen9(); JPanel panel1=new JPanel(); panel1.add(panel); this.add(panel1,BorderLayout.WEST); this.add(canvas,BorderLayout.CENTER); } public class AscultatorButon implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==infasoara){ canvas.setStare(1); System.out.println("AM apasat!");

Page 109: Laborator geometrie computationala

2

infasoara.setEnabled(false); } else if(e.getSource()==play){ //canvas.set(1); } } } } Algoritmul pt. problema 9 package prob9; import java.awt.*; import java.util.*; public class Algoritm9{ ArrayList puncte, puncte1, listanord, listasud; Punct9 a,b,c,d; public Algoritm9(ArrayList p){ puncte=new ArrayList(p); a=new Punct9(0,0); b=new Punct9(0,0); c=new Punct9(0,0); d=new Punct9(0,0); listanord=new ArrayList(); listasud=new ArrayList(); } private void sort(){ TreeSet p=new TreeSet(puncte); puncte=new ArrayList(p); //Arrays.sort(puncte); } public void algoritm() { //se sorteaza dupa absisa sort(); System.out.println("!!! - toate punctele sortate dupa abscisa - !!!"); for(int i=0;i<puncte.size();++i) { System.out.println(puncte.get(i)); } boolean[] nodVizitat=new boolean[puncte.size()];

Page 110: Laborator geometrie computationala

3

if(puncte.size()==0) return; int i=0; a=(Punct9)puncte.get(0); b=(Punct9)puncte.get(puncte.size()-1); nodVizitat[0]=true; listanord.add(a); c=a; System.out.println("Lista Nord"); while(c.compareTo(b)!=0) { for(int j=i+1;j<puncte.size();++j) { boolean ok=true; c=(Punct9)puncte.get(j); nodVizitat[j]=true; ok=true; for(int k=i+1;k<puncte.size();++k) { d=(Punct9)puncte.get(k); if(!nodVizitat[k]) if(det()>0) {ok=false;break; }; } if(ok) { listanord.add(c); a=c; for(int l=i+1;l<j;++l) nodVizitat[l]=true; i=j; break; } else nodVizitat[j]=false; } } System.out.println("Lista Sud"); i=0; c=new Punct9(0,0); d=new Punct9(0,0); nodVizitat=new boolean[puncte.size()]; //for(int ll=0;ll>nodVizitat.length;++ll) if(nodVizitat[0]==true) System.exit(0); a=(Punct9)puncte.get(0); b=(Punct9)puncte.get(puncte.size()-1); nodVizitat[0]=true; listasud.add(a); c=a; while(c.compareTo(b)!=0) {

Page 111: Laborator geometrie computationala

4

for(int j=i+1;j<puncte.size();++j) { boolean ok=true; c=(Punct9)puncte.get(j); nodVizitat[j]=true; ok=true; for(int k=i+1;k<puncte.size();++k) { d=(Punct9)puncte.get(k); if(!nodVizitat[k]) if(det()<0) {ok=false;break; }; } if(ok) { listasud.add(c); a=c; for(int l=i+1;l<j;++l) nodVizitat[l]=true; i=j; break; } else nodVizitat[j]=false; } } } public ArrayList getListaNord(){ System.out.println(listanord.size()+" Dimensiunea listei de nord!"); return listanord; } public double det() { return d.getX()*a.getY()+ a.getX()*c.getY()+ c.getX()*d.getY()- a.getY()*c.getX()- c.getY()*d.getX()- d.getY()*a.getX(); } public ArrayList getListaSud(){ System.out.println(listasud.size()+" Dimensiunea listei de sud!"); return listasud; } } Suprafata de desen pentru problema 9 package prob9; import javax.swing.*;

Page 112: Laborator geometrie computationala

5

import java.awt.*; import java.awt.event.*; import java.awt.Graphics.*; import java.util.*; public class Desen9 extends Canvas{ private ArrayList puncte,frontiera; private Vector x,y; private boolean potIntroducePuncte=true; private int stare; public Desen9(){ puncte=new ArrayList(); frontiera=new ArrayList(); x=new Vector(); y=new Vector(); stare=0; this.setSize(500,500); this.enableEvents(MouseEvent.MOUSE_CLICKED); } public void paint(Graphics g){ g.setColor(Color.BLACK); //chenarul g.drawLine(0,0,500,0); g.drawLine(0,0,0,500); g.drawLine(500,0,500,500); g.drawLine(0,500,500,500); //trasez axa x' g.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g.drawString("x",calcX(21),calcY(-1)); g.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y' g.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20)); g.drawString("y",calcX(-1),calcY(21)); g.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); //desenez punctele

Page 113: Laborator geometrie computationala

6

Iterator it=puncte.iterator(); while(it.hasNext()){ Punct9 punct=(Punct9) it.next(); g.fillOval(calcX(punct.getX())-2, calcY(punct.getY())-2,4,4); } //daca am apasat butonul "Infasoara" (deci starea canvasului este 1) if((stare==1) && puncte.size()>=3){ Algoritm9 a=new Algoritm9(puncte); a.algoritm(); ArrayList listanord=a.getListaNord(); int kNr=0; for(int i=0;i<listanord.size()-1;i++) { g.drawLine( calcX( ((Punct9)listanord.get(i)).getX() ), calcY( ((Punct9)listanord.get(i)).getY() ), calcX( ((Punct9)listanord.get(i+1)).getX() ), calcY( ((Punct9)listanord.get(i+1)).getY() )); g.drawString(""+(kNr++),calcX(((Punct9)listanord.get(i)).getX())+1,calcY(((Punct9)listanord.get(i)).getY())+1); } //g.drawLine( calcX (((Punct9)listanord.get(listanord.size()-1)).getX() ), calcY( ((Punct9)listanord.get(listanord.size()-1)).getY() ), calcX( ((Punct9)listanord.get(0)).getX() ), calcY( ((Punct9)listanord.get(0)).getY() )); ArrayList listasud=a.getListaSud(); for(int i=0;i<listasud.size()-1;i++) { g.drawLine( calcX( ((Punct9)listasud.get(i)).getX() ), calcY( ((Punct9)listasud.get(i)).getY() ), calcX( ((Punct9)listasud.get(i+1)).getX() ), calcY( ((Punct9)listasud.get(i+1)).getY() )); g.drawString(""+(kNr++),calcX(((Punct9)listasud.get(i+1)).getX())+1,calcY(((Punct9)listasud.get(i+1)).getY())+1); } //g.drawString(""+(kNr++),calcX(((Punct9)listasud.get(listasud.size()-1)).getX())+1,calcY(((Punct9)listasud.get(listasud.size()-1)).getY())+1); //g.drawLine( calcX (((Punct9)listasud.get(listasud.size()-1)).getX() ), calcY( ((Punct9)listasud.get(listasud.size()-1)).getY() ), calcX( ((Punct9)listasud.get(0)).getX() ), calcY( ((Punct9)listasud.get(0)).getY() )); } } public int calcX(double x){ return (int)(250+x*10);

Page 114: Laborator geometrie computationala

7

}//calcX(int x) public int calcY(double y){ return (int)(250-y*10); }//calcY(int y) public void setStare(int tip){ stare=tip; } public void processMouseEvent(MouseEvent e){ //daca apas pe mouse si nu am apasat inca butonul "Infasoara" (starea canvasului este 0) if( (e.getID()==MouseEvent.MOUSE_CLICKED) && (stare==0) ){ double Mx=(e.getX()-250)/10.0; double My=(250-e.getY())/10.0; Punct9 punct=new Punct9(Mx,My); puncte.add(punct); repaint(); } repaint(); } } Clasa ajutatoare. Creaza obiecte de tip Punct9 package prob9; import java.util.*; public class Punct9 implements Comparable{ private double x,y; public Punct9(double x, double y){ this.x=x; this.y=y; } public double getX(){ return x; } public double getY(){

Page 115: Laborator geometrie computationala

8

return y; } public void setX(int x){ this.x=x; } public void setY(int y){ this.y=y; } public int compareTo(Object o){ if( ((Punct9) o).getX() < this.getX() ) return +255; if( ((Punct9) o).getX() > this.getX() ) return -256; if( ((Punct9) o).getX() ==this.getX() ) if(y>((Punct9) o).getY()) return -255; if( ((Punct9) o).getX() ==this.getX() ) if(y<((Punct9) o).getY()) return +256; return 0; } public String toString() { return x+" "+y; } }

Page 116: Laborator geometrie computationala

1

Laborator 10

Algoritmul QuickHull pt. aflarea infasuratorii convexe. package prob10; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Problema10 extends JPanel{ private Desen10 canvas; private JButton infasoara, play; private AscultatorButon ab; public Problema10(){ this.setLayout(new BorderLayout()); ab=new AscultatorButon(); JPanel panel=new JPanel (new GridLayout(2,1)); infasoara=new JButton("Infasoara"); infasoara.addActionListener(ab); panel.add(infasoara); play=new JButton("Play"); play.addActionListener(ab); panel.add(play); play.setVisible(false); canvas=new Desen10(); JPanel panel1=new JPanel(); panel1.add(panel); this.add(panel1,BorderLayout.WEST); this.add(canvas,BorderLayout.CENTER); } public class AscultatorButon implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==infasoara){ canvas.setStare(1); System.out.println("AM apasat!");

Page 117: Laborator geometrie computationala

2

infasoara.setEnabled(false); } else if(e.getSource()==play){ //canvas.set(1); } } } } Algoritmul pt. problema 10 package prob10; import java.awt.*; import java.util.*; public class Algoritm10{ private TreeSet s; private TreeSet al; private Punct10 l,r; public Algoritm10(ArrayList s){ this.s=new TreeSet(s); } public void algoritm(){ double max=Integer.MIN_VALUE; double min=Integer.MAX_VALUE; Iterator i=s.iterator(); while(i.hasNext()){ Punct10 p=(Punct10) i.next(); if(p.getX()<min) {l=p; min=p.getX();} if(p.getX()>max) {r=p; max=p.getX();} } r=new Punct10(l.getX(),l.getY()-0.01); al=new TreeSet(); qHull(s, l, r); //al=new ArrayList(qHull(s, l, r)); System.out.println(al.size()); s.remove(r); } public TreeSet qHull(TreeSet s, Punct10 l, Punct10 r){ if((s.size()==2)&&(s.contains(l) && s.contains(r))) {

Page 118: Laborator geometrie computationala

3

al.add(s.first()); al.add(s.last()); return s; } else{ double det=-10000; Punct10 h=new Punct10(0,0); Iterator i=s.iterator(); while(i.hasNext()){ Punct10 p=(Punct10)i.next(); if(getDet(l,r,p)>det) {det=Math.abs(getDet(l,r,p)); h=p;} if(getDet(l,r,p)==det) if(p.getX()<h.getX()) { h=p;} } TreeSet s1=new TreeSet(); TreeSet s2=new TreeSet(); s1.add(l); s1.add(h); s2.add(r); s2.add(h); i=s.iterator(); while(i.hasNext()){ Punct10 p=(Punct10) i.next(); if(getDet(l,h,p)>0) s1.add(p); if(getDet(h,r,p)>0) s2.add(p); } TreeSet ss=new TreeSet(); ss.addAll(qHull(s1,l,h)); ss.addAll(qHull(s2,h,r)); ss.remove(h); return ss; } } public ArrayList getLista(){ System.out.println(al.size()); al.remove(r); return new ArrayList(al); } public double getDet(Punct10 l,Punct10 r,Punct10 p){ return (l.getX()*r.getY()+r.getX()*p.getY()+p.getX()*l.getY()-r.getY()*p.getX()- p.getY()*l.getX()-l.getY()*r.getX()); } }

Page 119: Laborator geometrie computationala

4

Suprafata de desen pentru problema 10 package prob10; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Graphics.*; import java.util.*; public class Desen10 extends Canvas{ private ArrayList puncte,frontiera; private Vector x,y; private boolean potIntroducePuncte=true; private int stare; public Desen10(){ puncte=new ArrayList(); frontiera=new ArrayList(); x=new Vector(); y=new Vector(); stare=0; this.setSize(500,500); this.enableEvents(MouseEvent.MOUSE_CLICKED); } public void paint(Graphics g){ g.setColor(Color.BLACK); //chenarul g.drawLine(0,0,500,0); g.drawLine(0,0,0,500); g.drawLine(500,0,500,500); g.drawLine(0,500,500,500); //trasez axa x' g.drawLine(calcX(-20),calcY(0),calcX(20),calcY(0)); g.drawString("x",calcX(21),calcY(-1)); g.drawLine(calcX(20)-3,calcY(0)-3,calcX(20),calcY(0)); g.drawLine(calcX(20)-3,calcY(0)+3,calcX(20),calcY(0)); //trasez axa y'

Page 120: Laborator geometrie computationala

5

g.drawLine(calcX(0),calcY(-20),calcX(0),calcY(20)); g.drawString("y",calcX(-1),calcY(21)); g.drawLine(calcX(0)+3,calcY(20)+3,calcX(0),calcY(20)); g.drawLine(calcX(0)-3,calcY(20)+3,calcX(0),calcY(20)); //desenez punctele Iterator it=puncte.iterator(); while(it.hasNext()){ Punct10 punct=(Punct10) it.next(); g.fillOval(calcX(punct.getX())-2, calcY(punct.getY())-2,4,4); } //daca am apasat butonul "Infasoara" (deci starea canvasului este 1) if((stare==1) && puncte.size()>=3){ Algoritm10 a=new Algoritm10(puncte); a.algoritm(); ArrayList listanord=a.getLista(); int kNr=0; System.out.println(listanord.size()+" dimensiunea listei inf. convexe"); for(int i=0;i<listanord.size()-1;i++) { g.drawLine( calcX( ((Punct10)listanord.get(i)).getX() ), calcY( ((Punct10)listanord.get(i)).getY() ), calcX( ((Punct10)listanord.get(i+1)).getX() ), calcY( ((Punct10)listanord.get(i+1)).getY() )); g.drawString(""+(kNr++),calcX(((Punct10)listanord.get(i)).getX())+1,calcY(((Punct10)listanord.get(i)).getY())+1); } g.drawLine( calcX (((Punct10)listanord.get(listanord.size()-1)).getX() ), calcY( ((Punct10)listanord.get(listanord.size()-1)).getY() ), calcX( ((Punct10)listanord.get(0)).getX() ), calcY( ((Punct10)listanord.get(0)).getY() )); g.drawString(""+(kNr++),calcX(((Punct10)listanord.get(listanord.size()-1)).getX())+1,calcY(((Punct10)listanord.get(listanord.size()-1)).getY())+1); } } public int calcX(double x){ return (int)(250+x*10); }//calcX(int x) public int calcY(double y){ return (int)(250-y*10); }//calcY(int y)

Page 121: Laborator geometrie computationala

6

public void setStare(int tip){ stare=tip; } public void processMouseEvent(MouseEvent e){ //daca apas pe mouse si nu am apasat inca butonul "Infasoara" (starea canvasului este 0) if( (e.getID()==MouseEvent.MOUSE_CLICKED) && (stare==0) ){ double Mx=(e.getX()-250)/10.0; double My=(250-e.getY())/10.0; Punct10 punct=new Punct10(Mx,My); puncte.add(punct); repaint(); } repaint(); } } Clasa ajutatoare. Creaza obiecte de tip Punct10 package prob10; import java.util.*; public class Punct10 implements Comparable{ private double x,y; public Punct10(double x, double y){ this.x=x; this.y=y; } public double getX(){ return x; } public double getY(){ return y; } public void setX(int x){ this.x=x; }

Page 122: Laborator geometrie computationala

7

public void setY(int y){ this.y=y; } public int compareTo(Object o){ if( ((Punct10) o).getX() == this.getX() && ((Punct10) o).getY() == this.getY() ) return 0; return +255; } public String toString() { return x+" "+y; } }


Recommended