Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please help me to correct
I have a line p0,p1 and its center point cp i want to draw a perpendicular through cp and its length is 8



C#
double ANG = GetAngleBetweenPoints(p0, p1);
g.DrawLine(currentPen, p0, p1);

                                cp = GetCenterPoint(p0, p1);
                                linangl = 180 - ANG;
                               


                                xa = Convert.ToInt32(cp.X + 8 * (Math.Cos(linangl)));
                                ya = Convert.ToInt32(cp.Y + 8 * (Math.Sin(linangl)));
                             
                               g.DrawLine(redPen, cp.X, cp.Y, xa, ya);
Posted
Comments
Peter_in_2780 2-Dec-12 22:09pm    
So what is your problem, exactly?
vishnulalr 2-Dec-12 22:25pm    
the above code can draw line but not perpendicular to p0,p1 the p0,p1 line may be inclined or straightline etc in any case i want to draw a perpendicular to p0,p1 but i getting sometimes inclined i want a perpendicular

Here's a basic elementary algebra solution. It relies in the equations you'd use in any basic linear algebra class.

There may be some issues with negatives, etc. you'll have to work out, and it doesn't work with horizontal lines (because the slope is 0 and the negative reciprocal of 0 is invalid). This code is just meant to demonstrate the theory behind this. You may also run into issues with the data types which can easily be resolved.

C#
Graphics surface = e.Graphics;

// Ready our variables and pick arbitrary points for the starter points.
Point p1 = new Point(30, 30);
Point p2 = new Point(50, 40);
Point midpoint = new Point();
Point p3 = new Point();
Point p4 = new Point();

double slope;

// Draw our original line.
surface.DrawLine(new Pen(Brushes.Black), p1, p2);

// Calculate the slope of the original line. (y2 - y1) / (x2 - x1) = slope
slope = ((double)(p2.Y - p1.Y) / (double)(p2.X - p1.X));

// Perpendicular lines have a slope of (-1 / originalSlope) -- the negative reciprocal.
slope = -1 / slope;

// Formula to find the midpoint.
midpoint.X = (p1.X + p2.X) / 2;
midpoint.Y = (p1.Y + p2.Y) / 2;

p3.X = midpoint.X;
p3.Y = midpoint.Y;

// Find the y-intercept of this equation. y=mx + b
double b = -slope * midpoint.X + midpoint.Y;

// Finally start calculating the final point.
// Add the length of the line to X.
p4.X = midpoint.X + 8;

// Now plug our slope, intercept, and new X into y=mx + b
p4.Y = (int)(slope * (midpoint.X + 8) + b);

// Draw that line!
surface.DrawLine(new Pen(Brushes.Blue), p3, p4);
 
Share this answer
 
v4
//
PcbGraph is picturebox control

//
PcbGraphPaint is picturebox paint event


private void PcbGraphPaint(object sender, PaintEventArgs e)
        {

            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(0, (pcbGraph.Height/2F)), new PointF(pcbGraph.Width, (pcbGraph.Height/2F)));
            var centerPoint = new PointF((pcbGraph.Width/2F), (pcbGraph.Height/2F));
            PointF p1;
            PointF p2;
            var value = FindCircleCircleIntersections(0, (pcbGraph.Height/2F), (pcbGraph.Width), pcbGraph.Width, (pcbGraph.Height/2F), (pcbGraph.Width), out p1, out p2);
            if (value != 2) return;
            e.Graphics.DrawLine(new Pen(Color.Black), centerPoint, p1);
            e.Graphics.DrawLine(new Pen(Color.Black), centerPoint, p2);

        }

        private int FindCircleCircleIntersections(float cx0, float cy0, float radius0, float cx1, float cy1, float radius1, out PointF intersection1, out PointF intersection2)
        {
            // Find the distance between the centers.  
            float dx = cx0 - cx1;
            float dy = cy0 - cy1;
            double dist = Math.Sqrt(dx*dx + dy*dy);
            // See how manhym solutions there are.  
            if (dist > radius0 + radius1)
            {
                // No solutions, the circles are too far apart.       
                intersection1 = new PointF(float.NaN, float.NaN);
                intersection2 = new PointF(float.NaN, float.NaN);
                return 0;
            }
            if (dist < Math.Abs(radius0 - radius1))
            {
                // No solutions, one circle contains the other.     
                intersection1 = new PointF(float.NaN, float.NaN);
                intersection2 = new PointF(float.NaN, float.NaN);
                return 0;
            }
            if ((dist == 0) && (radius0 == radius1))
            {
                // No solutions, the circles coincide.    
                intersection1 = new PointF(float.NaN, float.NaN);
                intersection2 = new PointF(float.NaN, float.NaN);
                return 0;
            }
            // Find a and h.    
            double a = (radius0*radius0 - radius1*radius1 + dist*dist)/(2*dist);
            double h = Math.Sqrt(radius0*radius0 - a*a);
            // Find P2.      
            double cx2 = cx0 + a*(cx1 - cx0)/dist;
            double cy2 = cy0 + a*(cy1 - cy0)/dist;
            // Get the points P3.      
            intersection1 = new PointF((float) (cx2 + h*(cy1 - cy0)/dist), (float) (cy2 - h*(cx1 - cx0)/dist));
            intersection2 = new PointF((float) (cx2 - h*(cy1 - cy0)/dist), (float) (cy2 + h*(cx1 - cx0)/dist));
            // See if we have 1 or 2 solutions.     
            if (dist == radius0 + radius1) return 1;
            return 2;
        }
 
Share this answer
 
Comments
vishnulalr 3-Dec-12 1:43am    
great its working thanks Mr Brajesh Kumar Singh .....:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900