Need help with Linear Equations.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bigs
    New Member
    • May 2007
    • 8

    #1

    Need help with Linear Equations.

    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.

    Code:
    if (Global.psize == 1)//Check If Line Should Be Graphed
    {
    Global.slope = x1i2;
    Global.yinter = y1i2;
    Global.yinter = Global.y1i1;// b=y For First Point On y Axis.
    pointx1 = (Global.x1i1 * 20 + (15*21));//Converts Window (x,y) To Graph (x,y)
    pointy1 = (Global.y1i1 * -20 + (15*21));//Converts Window (x,y) To Graph (x,y)
    pointx2 = (Global.x1i2 * 20 + (15*21));//Converts Window (x,y) To Graph (x,y)
    pointy2 = (Global.y1i2 * -20 + (15*21));//Converts Window (x,y) To Graph (x,y)
    g.fillOval(pointx1,pointy1,10,10);//Draw point
    g.fillOval(pointx2,pointy2,10,10);//Draw point
    }
    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...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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:[code=java]
    protected void paintComponent( Graphics g) {
    Graphics2D g2d= (Graphics2D)g;
    // use g2d for all drawing actions[/code]
    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

    Comment

    • Bigs
      New Member
      • May 2007
      • 8

      #3
      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?

      Comment

      • prometheuzz
        Recognized Expert New Member
        • Apr 2007
        • 197

        #4
        Originally posted by Bigs
        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.


        Originally posted by Bigs
        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.

        Comment

        • Bigs
          New Member
          • May 2007
          • 8

          #5
          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=?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Bigs
            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

            Comment

            • Bigs
              New Member
              • May 2007
              • 8

              #7
              Originally posted by JosAH
              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?

              Comment

              • prometheuzz
                Recognized Expert New Member
                • Apr 2007
                • 197

                #8
                Originally posted by Bigs
                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).

                Comment

                Working...