By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

Need help with Linear Equations.

P: 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
Share this Question
Share on Google+
7 Replies


Expert 10K+
P: 11,448
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

P: 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
Expert 100+
P: 197
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

P: 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

Expert 10K+
P: 11,448
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

P: 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
Expert 100+
P: 197
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

Post your reply

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