0

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);



        }
Community
  • 1
  • 1
junimcas
  • 1
  • 3
  • You might want to include links to what you've been looking at, it'll help us to better see where you've deviated! – LeonH Aug 25 '15 at 07:47
  • 1
    Thanks for your quick response. Some links are this:http://stackoverflow.com/questions/1922659/how-to-calculate-end-points-of-perpendicular-line-segments http://stackoverflow.com/questions/5224444/calculate-a-point-normal-to-a-line http://stackoverflow.com/questions/17195055/calculate-a-perpendicular-offset-from-a-diagonal-line – junimcas Aug 25 '15 at 08:30

1 Answers1

0

Someone told me my mistake and I share it if someone helps you. The problem was here:

normalizado.x = (int)Math.round(director.x / medida); normalizado.y = (int)Math.round(director.y / medida);

When rounding the result is invalid.For we round coordinates in the next step and it works properly. Thus:

                double normX = rotado.x / medida;
                double normY = rotado.y / medida;                   

                //SUMAR LO OBTENIDO AL PUNTO MEDIO
                resultadoMas.x = (int) Math.round(centro.x + (normX * altoArticulo));
                resultadoMas.y = (int) Math.round(centro.y + (normY * altoArticulo)); 
                resultadoMenos.x = (int) Math.round(centro.x - (normX * altoArticulo));
                resultadoMenos.y = (int) Math.round(centro.y - (normY * altoArticulo));

Thank you for your time and regreted.

junimcas
  • 1
  • 3