473,406 Members | 2,549 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,406 software developers and data experts.

Need help with Linear Equations.

8
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 4086
JosAH
11,448 Expert 8TB
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
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 Expert 100+
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
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 Expert 8TB
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
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 Expert 100+
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
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...
4
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
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
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 –...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.