473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Draw line,using equation ax + by + c = 0

Hi All,
could anyone help me with how to draw in picturebox (c#) line, using equation ax + by + c = 0. As a result from calculation I have calculate a, b,c, but now I need draw line to picturebox with this

I try calculate:
x=-c-y*b/a
z=-c-a*x/b

Expand|Select|Wrap|Line Numbers
  1.             double[] x = new double[2];
  2.             double[] y = new double[2];
  3.  
  4.             x[0] = (-c - 0 * b) / a;
  5.             x[1] = (-c - 1 * b) / a;
  6.  
  7.             y[0] = (-c - 1 * a) / b;
  8.             y[1] = (-c - 1 * a) / b;
  9.  
  10.             x[0] = Math.Abs(x[0] * 400);
  11.             x[1] = Math.Abs(x[1] * 400);
  12.  
  13.             y[0] = Math.Abs(400-y[0] * 400);
  14.             y[1] = Math.Abs(400-y[1] * 400);
  15.  
But when i draw line using points x and y, i thing that line is draw wrong.

Than you for help.
Apr 15 '11 #1
1 8993
GaryTexmo
1,501 Expert 1GB
I haven't done lines this way in a loooooong time haha, but I'll try to help you. As I understand it, a, b, and c define your line. Then you can calculate x for a given y, and y for a given x.

To calculate a, b, and c, you need two points. You can draw those two points to see the line between them. Create a new form and then override the OnPaint event. Make two points and draw them.

Expand|Select|Wrap|Line Numbers
  1. protected override void OnPaint(PaintEventArgs e)
  2. {
  3.     Graphics g = e.Graphics;
  4.  
  5.     Point p1 = new Point(50, 75);
  6.     Point p2 = new Point(300, 125);
  7.  
  8.     g.DrawLine(Pens.Black, p1, p2);
  9. }


Now you can calculate a, b, and c from those points.

Expand|Select|Wrap|Line Numbers
  1. float a = p1.Y - p2.Y;
  2. float b = p2.X - p1.X;
  3. float c = p1.X * p2.Y - p2.X * p1.Y;
Now we can calculate points... lets say we want to pick an x and calculate y. We need to rearrange our formula...

ax + by = 0
ax + by = -c
by = -c - ax
y = (-c - ax) / b
Keep in mind that b can't be zero when we do it this way. I believe this is the limitation of this method of line drawing. It maps every value of x to a single value for y. So if all your values of Y are at the same x value, there's no way to calculate it.

Anyway, now that we have a, b, and c, we can just pick values for X and calculate Y. Then we can draw them and see where they fit on our original line. This will tell us if we've made a mistake or not.

Expand|Select|Wrap|Line Numbers
  1. if (b != 0)
  2. {
  3.     float newX1 = 75;
  4.     float newY1 = (-c - a * newX1) / b;
  5.  
  6.     float newX2 = 275;
  7.     float newY2 = (-c - a * newX2) / b;
  8.  
  9.     g.DrawLine(Pens.Red, newX1, newY1, newX2, newY2);
  10. }



NOTE: I used this page as a reference...
http://bobobobo.wordpress.com/2008/0...ons-ax-by-c-0/

As you can see, we've done the calculations correctly and the calculated line lays correctly over top of the original line.

Since you didn't post any of your values, you'll need to do this same investigation on your own (which is good for you to do anyway, to be honest). See if your calculated line lays correctly over top of your original line. If it doesn't, check your math! This is actually what I had to do in generating this... the first couple of tries I found I wasn't getting the correct result, and it turned out I made a few math errors ;) Give it a try and if you're still having trouble, please post with more information so we can see what values you're using.
Attached Images
File Type: png pic1.PNG (3.5 KB, 3484 views)
File Type: png pic2.PNG (3.5 KB, 3403 views)
Apr 15 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: smilnet | last post by:
I want to Make a Draw Line Contrls,Please Help me Thanks!
18
by: Ed Bitzer | last post by:
Can draw a line on my form with a button click event but cannot upon the Load event when I wish. No more line object so used the following: Dim bit As Bitmap = New Bitmap(Me.Width, Me.Height) Dim...
4
by: thomasp | last post by:
I found the following code on MSDN to draw a line in VB2005. Public Sub DrawLinePoint(ByVal e As PaintEventArgs) ' Create pen. Dim blackPen As New Pen(Color.Black, 3) ' Create points that...
1
by: balakrishnan.dinesh | last post by:
Hi frnds, Im creating function to plot line graph in javascript . I have marked the points in graph. but what i need to do is, i want to draw the line between those marked point, but i dont...
4
by: =?Utf-8?B?bWFydGluMQ==?= | last post by:
Hi, All, I want to draw line chart on the web using visual basic 2005, vb 2005 doesn't have chart components, so can anyone point out where I can start working this? Thanks, Ma
2
by: Chetana | last post by:
Hi , I have created one application which has multiple lat and lon values . I want to draw a polyline for those point .I am not using google map so I want function in javascript to draw line....
2
by: NithyaMM | last post by:
Hi, Draw table using line between rows and Columns Help me
0
by: joshguru | last post by:
i have to draw a line by mouse after i clicked the button "draw line" in my form in vb.net....send me sample coding.........
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.