Obtener el punto perpendicular a una recta respecto a otro punto

En ocasiones puede que necesites conocer el punto de corte de un punto proyectado perpendicularmente a una línea recta. Con esta sencilla función podrás obtener ese punto.

A continuación el código de la función:

        /// <summary>

        /// Obtiene el punto perpendicular a una recta respecto a otro punto

        /// </summary>

        /// <param name=”pt“>Punto de referencia</param>

        /// <param name=”StartPoint“>Punto inicial de la recta</param>

        /// <param name=”EndPoint“>Punto final de la recta</param>

        /// <param name=”precision“>Grado de precisión decimal</param>

        /// <returns>El punto perpendicular a la recta representada por dos puntos</returns>

        public static Point3D PerpendPoint (Point3D pt, Point3D StartPoint, Point3D EndPoint, int precision)

        {

            double y = ((EndPoint.y – StartPoint.y) * (pt.x – StartPoint.x) –

                        (EndPoint.x – StartPoint.x) * (pt.y – StartPoint.y)) /

                        (Math.Pow(EndPoint.y – StartPoint.y, 2) +

                         Math.Pow(EndPoint.x – StartPoint.x, 2));

            double x = pt.x – (EndPoint.y * y – StartPoint.y * y);

            double num = pt.y + (EndPoint.x * y – StartPoint.x * y);

            double z = pt.z + (EndPoint.z * y – StartPoint.z * y);

            return new Point3D(x, num, z);

        }

Para poner a prueba la función puedes crear un formulario parecido al siguiente:

A continuación el código para los eventos de los controles para el formulario:

        private void txtPt1X_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), true, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void txtPt1Y_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), true, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void txtPt1Z_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), true, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void txtPt2X_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), true, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void txtPt2Y_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), true, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void txtPt2Z_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), true, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void txtPtX_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), true, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void txtPtY_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), true, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void txtPtZ_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), true, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void txtPrecision_KeyPress(object sender, KeyPressEventArgs e)

        {

            TextBox tb = sender as TextBox;

            string antT = tb.Text;

            string t = tb.Text;

            e.Handled = MyClase.OnlyNumbers(e.KeyChar.ToString(), false, ref t);

            if (antT != t)

            {

                tb.Text = t;

                tb.SelectionStart = tb.Text.Length;

            }

        }

 

        private void btCalcular_Click(object sender, EventArgs e)

        {

            this.lblMsg.Text = “Mensaje:”;

            this.lblX.Text = “0”;

            this.lblY.Text = “0”;

            this.lblZ.Text = “0”;

            // Verificar los valores en los TextBox

            if (!MyClase.IsNumeric(this.txtPt1X.Text) ||

                !MyClase.IsNumeric(this.txtPt1Y.Text) ||

                !MyClase.IsNumeric(this.txtPt1Z.Text) ||

                !MyClase.IsNumeric(this.txtPt2X.Text) ||

                !MyClase.IsNumeric(this.txtPt2Y.Text) ||

                !MyClase.IsNumeric(this.txtPt2Z.Text) ||

                !MyClase.IsNumeric(this.txtPtX.Text) ||

                !MyClase.IsNumeric(this.txtPtY.Text) ||

                !MyClase.IsNumeric(this.txtPtZ.Text) ||

                !MyClase.IsNumeric(this.txtPrecision.Text))

            {

                this.lblMsg.Text = “Mensaje: Formato erróneo en los números o en la precisión.”;

            }

            Point3D StartPoint = new Point3D(this.txtPt1X.ToDouble(), this.txtPt1Y.ToDouble(), this.txtPt1Z.ToDouble());

            Point3D EndPoint = new Point3D(this.txtPt2X.ToDouble(), this.txtPt2Y.ToDouble(), this.txtPt2Z.ToDouble());

            Point3D RefPoint = new Point3D(this.txtPtX.ToDouble(), this.txtPtY.ToDouble(), this.txtPtZ.ToDouble());

            double Precision = this.txtPrecision.ToInteger();

            Point3D PtResult = MyClase.PerpendPoint(RefPoint, StartPoint, EndPoint, Convert.ToInt16(Precision));

            this.lblX.Text = MyClase.Trunk(PtResult.x, Convert.ToInt16(Precision)).ToString();

            this.lblY.Text = MyClase.Trunk(PtResult.y, Convert.ToInt16(Precision)).ToString();

            this.lblZ.Text = MyClase.Trunk(PtResult.z, Convert.ToInt16(Precision)).ToString();

        }

Si todo ha ido bien deberías tener un resultado parecido al siguiente:

Ponlo a prueba, piensa en cómo mejorarlo y comparte con nosotros tus progresos.

 

Deja un comentario

Tu dirección de correo electrónico no será publicada.

Información básica sobre protección de datos Ver más

  • Responsable: MTB Software de Ponent, SLU.
  • Finalidad:  Moderar los comentarios.
  • Legitimación:  Por consentimiento del interesado.
  • Destinatarios y encargados de tratamiento:  No se ceden o comunican datos a terceros para prestar este servicio. El Titular ha contratado los servicios de alojamiento web a Hostinet, SL que actúa como encargado de tratamiento.
  • Derechos: Acceder, rectificar y suprimir los datos.
  • Información Adicional: Puede consultar la información detallada en la Política de Privacidad.

Esta web utiliza cookies propias y de terceros para su correcto funcionamiento y para fines analíticos. Al hacer clic en el botón Aceptar, acepta el uso de estas tecnologías y el procesamiento de tus datos para estos propósitos. Ver Política de cookies
Privacidad