// NOTE: please use a PRESERVE:BEGIN/PRESERVE:END comment block // to preserve your hand-coding across code generations. package pckMapa; import java.util.*; public class Mapa { private Vector vTrecho = new Vector(); public Vector vPonto = new Vector(); private String nomeArquivo = ""; public Mapa(String nomeArquivo) { setNomeArquivo (nomeArquivo); } public void setNomeArquivo(String val) { nomeArquivo = val; } /** * Dado um trecho t, possibilidades(t) devolve todos os trechos para onde pode-se ir a partir de t. * Para determinar possibilidades(t) considera-se: * * todos os trechos que passam pelo pontoFinal de t menos o próprio trecho * * retorna-se então todos os trechos onde o pontoFinal de t = pontoInicial dos demais trechos. */ public Trecho[] possibilidades( Trecho t ) { // PRESERVE:BEGIN // Insert your preservable code here... Trecho tt = procurarTrecho( t.getId() ); if (tt != null && tt.getFim() != null) { return tt.getInicio().getTrechos(); } return null; // PRESERVE:END } public Trecho[] possibilidades( int t) { return possibilidades( procurarTrecho(t) ); } /** * */ public boolean pode(Trecho t1, Trecho t2) { Trecho t[] = possibilidades(t1); for (int i=0; i < t.length; i++) if (t[i] == t2) return true; return false; } /** * */ public boolean pode(int t1, int t2) { Trecho tt1 = procurarTrecho(t1); Trecho tt2 = procurarTrecho(t2); return pode(tt1, tt2); } /** * */ public Trecho procurarTrecho(int id) { for (int i = 0; i< vTrecho.size(); i++) { Trecho t2 = (Trecho) vTrecho.elementAt( i ); if (t2.getId() == id) { return t2; } } return null; } /** * */ private Trecho criarTrecho(int id) { Trecho t = procurarTrecho(id); if (t == null) { t = new Trecho(); t.setId( id ); vTrecho.addElement( t ); } return t; } /** * */ private Ponto procurarPonto(double x, double y) { for (int i = 0; i< vPonto.size(); i++) { Ponto p2 = (Ponto) vPonto.elementAt( i ); if (p2.getX() == x && p2.getY() == y) { return p2; } } return null; } /** * */ private Ponto criarPonto(double x, double y) { Ponto p = procurarPonto(x, y); if (p==null) { p = new Ponto(x, y); vPonto.addElement( p ); } return (p); } /** * */ public void carregarMapa() { // PRESERVE:BEGIN // Insert your preservable code here... try { Vector vt = (new LeitorArquivo()).mtObtemDadosArquivo(nomeArquivo + ".trechos"); for (int x = 1; x < vt.size(); x++) { Vector vtt = (Vector)vt.elementAt(x); Trecho t1 = criarTrecho( Integer.parseInt((String)vtt.elementAt(0) ) ); } } catch (Exception e) { e.printStackTrace(); } PegaEsquinas.nomeArquivo = nomeArquivo + ".esquinas"; PegaEsquinas pEsquina = new PegaEsquinas(); Vector vetorEsquinas = pEsquina.vetorEsquinas; System.out.println(vetorEsquinas.size()); for (int i=0;i < vetorEsquinas.size();i++){ Esquina esquina = (Esquina)vetorEsquinas.elementAt(i); Ponto p = criarPonto( esquina.getPosicaoX(), esquina.getPosicaoY() ); Vector saem = new Vector(); saem = esquina.getTrechosSaem(); for (int j=0; j < saem.size(); j++) { Integer trechoSae = (Integer)saem.elementAt(j); Trecho t = criarTrecho( trechoSae.intValue() ); p.adicionarTrecho( t, false ); } Vector entram = new Vector(); entram = esquina.getTrechoChegam(); for (int j=0; j < entram.size(); j++) { Integer trechoEnt = (Integer)entram.elementAt(j); Trecho t = criarTrecho( trechoEnt.intValue() ); p.adicionarTrecho( t, true ); } } // PRESERVE:END } /** * */ public Trecho[] getTrechos() { Trecho t[] = new Trecho[ vTrecho.size() ]; vTrecho.copyInto( t ); return t; } /** * */ public Trecho getTrecho(int id) { return procurarTrecho( id ); } /** * */ public Ponto[] getPontos() { Ponto p[] = new Ponto[ vPonto.size() ]; vPonto.copyInto( p ); return p; } /** * */ public static void main(String args[]) { Mapa m = new Mapa( (args.length >= 1) ? args[0] : "unicamp" ); System.out.println( "Inicio" ); m.carregarMapa(); Trecho t[] = m.possibilidades( 345 ); if (t != null) { System.out.print( "Possibilidades: " ); for (int i=0; i < t.length; i++) System.out.println( t[i].getId() ); } System.out.println( "Fim" ); } }