private static IList Recortar(IList listado, Punto vertice1, Punto vertice2) { IList listadoClip = new ArrayList(); if (listado.Count == 0) return listadoClip; Punto s = (Punto)listado[listado.Count - 1]; foreach (Punto p in listado) { if (EsInterior(p, vertice1, vertice2)) { if (EsInterior(s, vertice1, vertice2)) { listadoClip.Add(p); } else { listadoClip.Add(Interseccion(s, p, vertice1, vertice2)); listadoClip.Add(p); } } else if (EsInterior(s, vertice1, vertice2)) { listadoClip.Add(Interseccion(s, p, vertice1, vertice2)); } s = p; } return listadoClip; }
private static bool EsInterior(Punto p, Punto vertice1, Punto vertice2) { if (vertice2.GetXFlotante() > vertice1.GetXFlotante()) //inferior { if (p.GetYFlotante() >= vertice1.GetYFlotante()) { return true; } } if (vertice2.GetXFlotante() < vertice1.GetXFlotante()) //superior { if (p.GetYFlotante() <= vertice1.GetYFlotante()) { return true; } } if (vertice2.GetYFlotante() > vertice1.GetYFlotante()) //derecha { if (p.GetXFlotante() <= vertice2.GetXFlotante()) { return true; } } if (vertice2.GetYFlotante() < vertice1.GetYFlotante()) //izquierda { if (p.GetXFlotante() >= vertice2.GetXFlotante()) { return true; } } return false; }
private static Punto Interseccion(Punto s, Punto p, Punto vertice1, Punto vertice2) { double x, y; if (vertice1.GetYFlotante() == vertice2.GetYFlotante()) //Limite vertical { y = vertice1.GetYFlotante(); x = s.GetXFlotante() + (vertice1.GetYFlotante() - s.GetYFlotante()) * (p.GetXFlotante() - s.GetXFlotante()) / (p.GetYFlotante() - s.GetYFlotante()); } else //Limite vertical { x = vertice1.GetXFlotante(); y = s.GetYFlotante() + (vertice1.GetXFlotante() - s.GetXFlotante()) * (p.GetYFlotante() - s.GetYFlotante()) / (p.GetXFlotante() - s.GetXFlotante()); } return new PuntoFlotante(x, y); }
public PuntoFlotante(double x, double y, Punto origenCoordenadas) { this.X = x; this.Y = y; this.OrigenCoordenadas = origenCoordenadas; }
public Linea(Punto inicio, Punto fin) { this.Inicio = inicio; this.Fin = fin; }
public PuntoEntero(int x, int y, Punto origenCoordenadas) { this.X = x; this.Y = y; this.OrigenCoordenadas = origenCoordenadas; }
public Circulo(Punto centro, double radio) { this.Centro = centro; this.Radio = radio; }
private static void procesarSegmento(Punto inicial, Punto final, Dictionary<int, ArrayList> segmentos) { int dy = final.GetYEntero() - inicial.GetYEntero(); int dx = final.GetXEntero() - inicial.GetXEntero(); int stepX = 1; int stepY = 1; if (dx < 0) { //el x final es menor al inicial, entonces doy vuelta el punto. //esto lo logro invirtiendo el orden de recorrida de los puntos //y por lo tanto doy vuelta el dx stepX = -1; dx = -dx; } if (dy < 0) { //el y final es menor al inicial, entonces doy vuelta el punto. //esto lo logro invirtiendo el orden de recorrida de los puntos //y por lo tanto doy vuelta el dy stepY = -1; dy = -dy; } int x = inicial.GetXEntero(); int y = inicial.GetYEntero(); if (Math.Abs(dy) < Math.Abs(dx)) { int error = 2 * dy - dx; int errorE = 2 * dy; int errorD = 2 * (dy - dx); bool sentidoCreciente = (dx == 1); int minX = x; int maxX = x; while (x != (final.GetXEntero() + stepX)) { minX = x; maxX = x; if (x < minX) minX = x; if (x > maxX) maxX = x; x += stepX; if (error < 0) { error += errorE; } else { if (sentidoCreciente) { agregarPunto(segmentos, maxX, y); } else { agregarPunto(segmentos, minX, y); } error += errorD; y += stepY; } } if (segmentos.ContainsKey(inicial.GetYEntero()) && segmentos[inicial.GetYEntero()].Contains(inicial.GetXEntero())) { segmentos[inicial.GetYEntero()].RemoveAt(segmentos[inicial.GetYEntero()].LastIndexOf(inicial.GetXEntero())); } if (segmentos.ContainsKey(final.GetYEntero()) && segmentos[final.GetYEntero()].Contains(final.GetXEntero())) { segmentos[final.GetYEntero()].RemoveAt(segmentos[final.GetYEntero()].LastIndexOf(final.GetXEntero())); } } else { int error = 2 * dx - dy; int errorE = 2 * dx; int errorD = 2 * (dx - dy); while (y != (final.GetYEntero() + stepY)) { agregarPunto(segmentos, x, y); y += stepY; if (error < 0) { error += errorE; } else { error += errorD; x += stepX; } } segmentos[inicial.GetYEntero()].RemoveAt(segmentos[inicial.GetYEntero()].LastIndexOf(inicial.GetXEntero())); segmentos[final.GetYEntero()].RemoveAt(segmentos[final.GetYEntero()].LastIndexOf(final.GetXEntero())); } }