# Need help with Linear Equations.

Emmanuel Xoc

Greenhorn

Posts: 4

posted 9 years ago

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.

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...

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

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...

Campbell Ritchie

Sheriff

Posts: 50171

79

posted 9 years ago

If you are simply drawing a line, you can use a MouseListener to pick up the points where the mouse is clicked, and draw a line between the two points.

To get

I can't remember how you get

To get

*m*use*1.0 * (y2 - y1) / (x2 - x1)*. The reason for the*1.0*is to force an implicit cast to double.I can't remember how you get

*b*; I think it would be*y1 - x1 * m*.
Campbell Ritchie

Sheriff

Posts: 50171

79

posted 9 years ago

Or you can draw a line between the two points

The reason for the

[edit]Change

[ May 25, 2007: Message edited by: Campbell Ritchie ]

*(0, (int)-b)*and*(w, (int)(-w * m - b))*, where*w*is the width of the component.The reason for the

**-**is that the*y*coordinates go from top to bottom.[edit]Change

*y*to*b*in two places and add an (int) cast[/edit][ May 25, 2007: Message edited by: Campbell Ritchie ]

Emmanuel Xoc

Greenhorn

Posts: 4

posted 9 years ago

OK, here is a revised part (still not working):

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, 2007: Message edited by: Emmanuel Xoc ]

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, 2007: Message edited by: Emmanuel Xoc ]

Campbell Ritchie

Sheriff

Posts: 50171

79

Emmanuel Xoc

Greenhorn

Posts: 4

posted 9 years ago

You have the slope and the intercept. by definition that means that one point is (0,b).

If all you need is another point, just pick any value for x you want - say, 1. then, stick it into the slope/intercept formula, and calculate y. you now have two points.

example: Slope = 0.2 Intercept = 4

point 1 = ( 0 , 4 )

to find point 2, pick a value for x2, say 1.

i know that y2 = (m)(x2) + b. so, y2 = (0.2) (1) + 4 = 0.2 + 4 = 4.2

point 2 = ( 1 , 4.2 )

[ May 25, 2007: Message edited by: Fred Rosenberger ]

If all you need is another point, just pick any value for x you want - say, 1. then, stick it into the slope/intercept formula, and calculate y. you now have two points.

example: Slope = 0.2 Intercept = 4

point 1 = ( 0 , 4 )

to find point 2, pick a value for x2, say 1.

i know that y2 = (m)(x2) + b. so, y2 = (0.2) (1) + 4 = 0.2 + 4 = 4.2

point 2 = ( 1 , 4.2 )

[ May 25, 2007: Message edited by: Fred Rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Campbell Ritchie

Sheriff

Posts: 50171

79

posted 9 years ago

Are you sure it is (0, 0.2) Fred?

I would have thought it is (0, 4), only using positive numbers will make the graph display upside down. Agree the next point is (1, 4.2).

You have

Start off with

Go to x = 100. You now want to move 100 px to the right, so the first half of the other point is increased by 100, becoming

Try that.

I would have thought it is (0, 4), only using positive numbers will make the graph display upside down. Agree the next point is (1, 4.2).

You have

*y = bx + m*. You want to put it in a JPanel*w*pixels wide and*h*pixels high. You want the "origin" (0, 0) to appear in the centre of the screen. And remember you have to change the sign of the*y*values because*y = 0*is the top of the screen.Start off with

*x = 0*. The*y*value is the same as*m*. Because your*y*coordinates run downwards, use (w / 2, h / 2 - m).Go to x = 100. You now want to move 100 px to the right, so the first half of the other point is increased by 100, becoming

*w / 2 + 100*. You want to be 100*m*pixels**higher**than the origin, so the second part of the point is**reduced**by*100 * m*, becoming*h - 2 - m - 100 * m*. [You can see the formula can easily be simplified more.]Try that.

Campbell Ritchie

Sheriff

Posts: 50171

79

posted 9 years ago

Try this in your JPanel:-

[edit]There is a mistake in this formula somewhere. Sorry. I shall try again.[edit]

[ May 25, 2007: Message edited by: Campbell Ritchie ]

[edit]There is a mistake in this formula somewhere. Sorry. I shall try again.[edit]

[ May 25, 2007: Message edited by: Campbell Ritchie ]

posted 9 years ago

Nope. too early in the morning.

thanks. and are YOU sure it's y = bx + m?

I guess the question to the OP is do you need help with the math of y=mx+b, or with the translation of the cartesian coordinates into the JAVA coordinate system?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Originally posted by Campbell Ritchie:

[QB]Are you sure it is (0, 0.2) Fred?

Nope. too early in the morning.

thanks. and are YOU sure it's y = bx + m?

I guess the question to the OP is do you need help with the math of y=mx+b, or with the translation of the cartesian coordinates into the JAVA coordinate system?

Campbell Ritchie

Sheriff

Posts: 50171

79

colin shuker

Ranch Hand

Posts: 750

posted 9 years ago

I'm sure its not that difficult to do..

Perhaps using the other form for a straight line would be easier.

y-y1=m(x-x1)

I guess it depends on the JPanel size you are displaying it in, the location of the origin of the graph in the JPanel, and also the scale on the axes, but its really just drawing a line.

Perhaps using the other form for a straight line would be easier.

y-y1=m(x-x1)

I guess it depends on the JPanel size you are displaying it in, the location of the origin of the graph in the JPanel, and also the scale on the axes, but its really just drawing a line.