Hello I need to find a perpendicular line to another. I have one initial point(x,y), one final point(x, y) and a measure for de perpendicular line. I have tried in various ways I've seen on this page but something I'm doing wrong. Whenever I get a line perpendicular to the axis Y o axis X instead of my line. Does anyone know tell me I'm doing wrong?
Some links have been looking at are:
calculate a perpendicular offset from a diagonal line
How to calculate end points of perpendicular line segments?
Calculate a point normal to a line
Thank you very much.
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent arg0) {
////PRUEBAS CON ARCOS
///PRUEBAS CON PERPENDICULARES
Point centro = new Point(0, 0);
Point director = new Point(0, 0);
Point normalizado = new Point(0, 0);
Point escalado = new Point(0, 0);
Point rotado = new Point(0, 0);
Point resultadoMas = new Point(0, 0);
Point resultadoMenos = new Point(0, 0);
int coordx1;
int coordy1;
int coordx2;
int coordy2;
int coordx3;
int coordy3;
int coordx4;
int coordy4;
int altoArticulo = 80;
coordx1 = 100;
coordy1 = 100;
coordx2 = 600;
coordy2 = 300;
//CENTRO
centro.x = (coordx1 + coordx2)/2;
centro.y = (coordy1 + coordy2)/2;
//VECTOR DIRECTOR
director.x = centro.x - coordx1;
director.y = centro.y - coordy1;
//MEDIDA
double medida = Math.sqrt(((Math.pow(director.x, 2)) + (Math.pow(director.y, 2))));
//NORMALIZAR VECTOR
normalizado.x = (int)Math.round(director.x / medida);
normalizado.y = (int)Math.round(director.y / medida);
//ROTACIÓN 90 GRADOS
rotado.x = (normalizado.y * -1);
rotado.y = normalizado.x;
//SUMAR LO OBTENIDO AL PUNTO MEDIO
resultadoMas.x = centro.x + (rotado.x * altoArticulo);
resultadoMas.y = centro.y + (rotado.y * altoArticulo);
resultadoMenos.x = centro.x - (rotado.x * altoArticulo);
resultadoMenos.y = centro.y - (rotado.y * altoArticulo);
//punto4.x = (int) Math.round(punto3.x + ((Math.signum(vectorDirector.y) * -1) * ((Math.sin(Math.toRadians(anguloEjeY)) * mideEscalado))));
//punto4.y = (int) Math.round(punto3.y + ((Math.signum(vectorDirector.y) * -1) * ((Math.cos(Math.toRadians(anguloEjeY)) * mideEscalado))));
arg0.gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
arg0.gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
arg0.gc.drawLine(coordx1, coordy1, coordx2, coordy2);
arg0.gc.drawLine(resultadoMas.x, resultadoMas.y, resultadoMenos.x, resultadoMenos.y);
int[] comopinto = {resultadoMas.x, resultadoMas.y, resultadoMas.x + 10, resultadoMas.y + 10, resultadoMas.x - 10, resultadoMas.y + 10};
arg0.gc.drawPolygon(comopinto);
arg0.gc.fillPolygon(comopinto);
int[] comopinto2 = {resultadoMenos.x, resultadoMenos.y, resultadoMenos.x + 10, resultadoMenos.y - 10, resultadoMenos.x - 10, resultadoMenos.y - 10};
arg0.gc.drawPolygon(comopinto2);
arg0.gc.fillPolygon(comopinto2);
}