Chamfer two lines by give distances using C#

185 Views Asked by At

I am working on a simple tool in C#. I have three points which makes two lines meeting at point P. So that PP1 and PP2. I want to Chamfer the lines at meeting point such that distance d1 is trimmed from line PP1 and distance d2 is trimmed from line PP2 and then join the trimmed lines. I have Problem as i can't get exact result. Any idea whats the problem in my code. Thanks Result1 Result2

private void Chamfer(Graphics g,PointF P,PointF P1,PointF P2,double d1,double d2)
    {

        //Vector 1 Length
        double PP1 = Math.Sqrt((Math.Pow((P.X - P1.X), 2) + Math.Pow((P.Y - P1.Y), 2)));
        //Vector 2 Length
        double PP2 = Math.Sqrt((Math.Pow((P.X-P2.X), 2) + Math.Pow((P.Y - P2.Y), 2)));
        //Slopes & Angles
        double m1 = (P.Y-P1.Y) / (P.X - P1.X);
        double angle1 = Math.Atan(m1) * (180 / Math.PI);
        double m2 = (P.Y - P2.Y) / (P.X - P2.X);
        double angle2 = Math.Atan(m2) * (180 / Math.PI);
        //Coordinates of points of Chamfer

        if(P1.X>P.X && P1.Y > P.Y)
        {
            int Pd1X = Convert.ToInt32(P.X + d1 * Math.Cos(angle1));
            int Pd1Y = Convert.ToInt32(P.Y + d1 * Math.Sin(angle1));
            Chp1 = new Point(Pd1X, Pd1Y);
        }

        else if (P1.X > P.X && P1.Y < P.Y)
        {
            int Pd1X = Convert.ToInt32(P.X + d1 * Math.Cos(angle1));
            int Pd1Y = Convert.ToInt32(P.Y - d1 * Math.Sin(angle1));
            Chp1 = new Point(Pd1X, Pd1Y);
        }

        else if (P1.X < P.X && P1.Y < P.Y)
        {
            int Pd1X = Convert.ToInt32(P.X - d1 * Math.Cos(angle1));
            int Pd1Y = Convert.ToInt32(P.Y - d1 * Math.Sin(angle1));
            Chp1 = new Point(Pd1X, Pd1Y);
        }
        else if (P1.X < P.X && P1.Y > P.Y)
        {
            int Pd1X = Convert.ToInt32(P.X - d1 * Math.Cos(angle1));
            int Pd1Y = Convert.ToInt32(P.Y + d1 * Math.Sin(angle1));
            Chp1 = new Point(Pd1X, Pd1Y);
        }

        if (P2.X > P.X && P2.Y > P.Y)
        {
            int Pd2X = Convert.ToInt32(P.X + d2 * Math.Cos(angle2));
            int Pd2Y = Convert.ToInt32(P.Y + d2 * Math.Sin(angle2));
            Chp2 = new Point(Pd2X, Pd2Y);
        }
        else if (P2.X > P.X && P2.Y < P.Y)
        {
            int Pd2X = Convert.ToInt32(P.X + d2 * Math.Cos(angle2));
            int Pd2Y = Convert.ToInt32(P.Y - d2 * Math.Sin(angle2));
            Chp2 = new Point(Pd2X, Pd2Y);
        }
        else if (P2.X < P.X && P2.Y < P.Y)
        {
            int Pd2X = Convert.ToInt32(P.X - d2 * Math.Cos(angle2));
            int Pd2Y = Convert.ToInt32(P.Y - d2 * Math.Sin(angle2));
            Chp2 = new Point(Pd2X, Pd2Y);
        }
        else if (P2.X < P.X && P2.Y > P.Y)
        {
            int Pd2X = Convert.ToInt32(P.X - d2 * Math.Cos(angle1));
            int Pd2Y = Convert.ToInt32(P.Y + d2 * Math.Sin(angle1));
            Chp2 = new Point(Pd2X, Pd2Y);
        }

        Pen penPre = new Pen(Color.Green);
        penPre.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
        g.Clear(this.BackColor);
        g.DrawLine(Pens.Black, P1, Chp1);
        g.DrawLine(penPre, P1, P);
        g.DrawLine(penPre, P2, P);
        g.DrawString("P1", this.Font, Brushes.Red, new Point(Convert.ToInt32(P1.X + 2), Convert.ToInt32(P1.Y - 2)));
        g.DrawLine(Pens.Black, P2, Chp2);
        g.DrawString("P2", this.Font, Brushes.Red, new Point(Convert.ToInt32(P2.X + 2), Convert.ToInt32(P2.Y - 2)));
        g.DrawString("P", this.Font, Brushes.Red, new Point(Convert.ToInt32( P.X + 3),Convert.ToInt32( P.Y - 2)));

        g.DrawLine(Pens.Black, Chp1, Chp2);

    }

what i got at a specific coordinates, my code is working as you can see from photo attached. I can't figure if i don't have these golden coordinate. Result from golden coordinates

1

There are 1 best solutions below

0
On
 //Vector 1 Length
    double PP1 = Math.Sqrt((Math.Pow((P.X - P1.X), 2) + Math.Pow((P.Y - P1.Y), 2)));

    //unit direction vector
    upx1 = (P1.X - P.X) / PP1
    upy1 = (P1.Y - P.Y) / PP1

    // chpoint coordinates
    chx1 = P.X + upx1 * d;
    chy1 = P.Y + upy1 * d;

   // now the same for PP2