473,657 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with Linear Equations.

8 New Member
Ok, I have been working on a Linear Equation program that will draw a line on my graph. But, I am not sure how to set up the y=mx+b in Java to return the (x1,y1) and (x2, y2) using only the slope and y-intercept.

Can anyone tell me how to set up the linear equation in java.

Expand|Select|Wrap|Line Numbers
  1. if (Global.psize == 1)//Check If Line Should Be Graphed
  2. {
  3. Global.slope = x1i2;
  4. Global.yinter = y1i2;
  5. Global.yinter = Global.y1i1;// b=y For First Point On y Axis.
  6. pointx1 = (Global.x1i1 * 20 + (15*21));//Converts Window (x,y) To Graph (x,y)
  7. pointy1 = (Global.y1i1 * -20 + (15*21));//Converts Window (x,y) To Graph (x,y)
  8. pointx2 = (Global.x1i2 * 20 + (15*21));//Converts Window (x,y) To Graph (x,y)
  9. pointy2 = (Global.y1i2 * -20 + (15*21));//Converts Window (x,y) To Graph (x,y)
  10. g.fillOval(pointx1,pointy1,10,10);//Draw point
  11. g.fillOval(pointx2,pointy2,10,10);//Draw point
  12. }
This is a snip bit of my program. First it checks if the user wants to graph something, then it takes the data from the two fields the user entered, [ This part need help on. Getting it to find (x1,y1) and (x2, y2) ], then it converts the answer to points that will match up with the graph lines, draws the data on the graph etc...
May 25 '07 #1
7 4100
JosAH
11,448 Recognized Expert MVP
You don't need to convert yout coordinate system from "world coordinates" to
"device coordinates". Read all about it in the Graphics2D API documentation.

The Graphics object passed to your drawing methods actually is a Graphics2D
object, so all you have to do is cast it:
Expand|Select|Wrap|Line Numbers
  1. protected void paintComponent(Graphics g) {
  2. Graphics2D g2d= (Graphics2D)g;
  3. // use g2d for all drawing actions
I don't understand your problem with that line given the slope m and intercept b:
y= m*x+b and x= (y-b)/m, so for a given x you can easily find the corresponding
y value and for a given y you can find the x value. What more do you want?

kind regards,

Jos
May 25 '07 #2
Bigs
8 New Member
Here is my full program source http://www.badongo.com/file/3171822
I have been using BlueJ to write my java program.

Clarification on problem:
I have both m and b all ready.
I need to find the other points (x1,y1)(x2,y2) to draw the line.

So, how do I set up the formulas to work in Java to get the other points (x1,y1)(x2,y2) by using m and b?
May 25 '07 #3
prometheuzz
197 Recognized Expert New Member
Here is my full program source http://www.badongo.com/file/3171822
I have been using BlueJ to write my java program.
That won't be necessary.


Clarification on problem:
I have both m and b all ready.
I need to find the other points (x1,y1)(x2,y2) to draw the line.

...
There are infinite points laying on your line. You can pick an arbitrary x (or y) and calculate the corresponding y (or x) value using the formula's Jos already provided.
May 25 '07 #4
Bigs
8 New Member
You can pick an arbitrary x (or y) and calculate the corresponding y (or x) value using the formula's Jos already provided.

What do you mean? Won't picking a arbitrary x,y for the other point just give me a nearly random line?

Could you possibly show an example? I am very lost.
y1=b
x1=(y-b)/m
y2=?
x2=?
May 25 '07 #5
JosAH
11,448 Recognized Expert MVP
Here is my full program source http://www.badongo.com/file/3171822
I have been using BlueJ to write my java program.

Clarification on problem:
I have both m and b all ready.
I need to find the other points (x1,y1)(x2,y2) to draw the line.

So, how do I set up the formulas to work in Java to get the other points (x1,y1)(x2,y2) by using m and b?
Using the Graphics2D object (see my previous reply) use the scale() method
to scale your device coordinates and use the translate() method to set a new
origin and then just draw whatever you like using your own coordinate system.
Read the API docs for the Graphis2D class and the docs for the
AffineTransform class to see how scaling and translation works.

kind regards,

Jos
May 25 '07 #6
Bigs
8 New Member
Using the Graphics2D object (see my previous reply) use the scale() method
to scale your device coordinates and use the translate() method to set a new
origin and then just draw whatever you like using your own coordinate system.
Read the API docs for the Graphis2D class and the docs for the
AffineTransform class to see how scaling and translation works.

kind regards,

Jos
I will look into that.
But, how do I find (x1,y1)(x2,y2) from the slope and y-intercept?
May 25 '07 #7
prometheuzz
197 Recognized Expert New Member
I will look into that.
But, how do I find (x1,y1)(x2,y2) from the slope and y-intercept?
Let say you have slope = 3, and y-intercept = 4:



You pick two arbitrary points on the x-axis, let's say x1 = -2 and x2 = 2.
The corresponding y values can be calculated as this:

y1 = 3 * -2 + 4 = -2
y2 = 3 * 2 + 4 = 10

So, you have the two points: (-2, -2) and (2, 10).
May 25 '07 #8

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

Similar topics

1
4249
by: Guenther Sohler | last post by:
I am looking for a apropriate c library to solve my sparse linear equation system. I have n variables and m equations. m might be less, equal or greater then n!!!! If there are less equations than variables, the system should effectively set undefined variables to 0
4
9347
by: Thomas | last post by:
Hi, I've to solve a system of linear equations and inequations in C++. Are there (free) librarys for that topic? Regards, Thomas
3
1084
by: mano | last post by:
Hello.... Can anybody give me some idea ... How to write programs on (...(In C++)) Gauss elimination LU Decomposition finding Inverse Thanks
1
3202
by: HypeBeast McStreetwear | last post by:
Hello everyone. I got a assignment that states. The set of linear equations a11X1 = a12X2 = c1 a21X1 = a22X2 = c2 May be solved using Cramer’s rule: X1 = c1a22 – c2a12 a11a22 – a12a21
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.