Announcement

Collapse
No announcement yet.

need codes to find equation of straight line y = mx + b

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • need codes to find equation of straight line y = mx + b

    Hi everyone

    Are there any codes available in this forum to get a linear equation y= mx + b when given 2 points (x1,y1) and (x2,y2) ?
    and to determine whether a third point (x3, y3) lies on this line ?

    I'm not too well educated thus I need to catch up.

  • #2
    Howdy, Tim!

    This page explains the steps ...

    https://www.mathsisfun.com/algebra/l...n-2points.html

    Comment


    • #3
      Tim,
      Code:
      Function PBMain
       Local x1, y1, x2, y2 As Single   'two particular points
       Local winC As Dword              'graphic window handle
       Local x As Single, y As Single   'general point on line
      
      ' find
       Local m As Single    'slope
       Local b As Single    'y intercept
      
       ' Setup graphic window, give console focus so catches keystrokes
       Graphic Window "", 0, 0, 800, 800 To winC
        Graphic Attach winC, 0, ReDraw
       Graphic Scale (-400,400)-(400,-400)       'top window uses -241, bottom 249
       Console Set Loc 850, 0
       Console Set Focus
      
       Do
      
        Graphic Clear
      
        'random pairs of points
        x1 = 100 * Rnd - 100 : y1 = 100 * Rnd - 100
        x2 = 100 * Rnd - 100 : y2 = 100 * Rnd - 100
      
        ' Check if line is vertical (slope infinite)
        If x1 = x2 Then
         For y = -300 To 300
          Graphic Set Pixel(x1, y), %Black
         Next
         GoTo GRD
        End If
      
        m = (y2 - y1) / (x2 - x1)
      
        ' Now we know the m in
        '   y = mx + b
        ' To find b, plug in one of the points, say (x1, y1), and solve for b
        b = y1 - m * x1
      
        ' Draw the line
        For x = -300 To 300
         y = m * x + b
         Graphic Set Pixel(x, y), %Black
        Next
      
      GRD:
        ' Empahsize two give points
        x = x1 : y = y1
        Graphic Ellipse (x - 2, y - 2) - (x + 3, y + 3), %Red, %Red
        x = x2 : y = y2
        Graphic Ellipse (x - 2, y - 2) - (x + 3, y + 3), %Red, %Red
      
        Graphic ReDraw
      
       Loop Until WaitKey$ = $Esc
      
      End Function
      Last edited by Mark Hunter; 10 May 2022, 09:20 PM. Reason: corrections per Mike Doty and Dale Yarker
      Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

      Comment


      • #4
        Mark gives a solution.
        Might visually test your points on a line using \samples\ddt\graphic\PwrClock\PwrClock.bas

        Comment


        • #5
          Mark,
          GoTo GRD not found

          Comment


          • #6
            Why GRAPHIC REDRAW? GRAPHIC ATTACH does not have REDRAW option.

            Cheers,
            Dale

            Comment


            • #7
              Thank you Sir Mark, can you please make the code in PBWin10 as I don't have PBCC?

              Comment


              • #8
                delete 2 CONSOLE SET lines
                Add LOCAL InStr AS STRING
                Change exit loop to -
                InStr = GRAPHIC$(INKEY$)
                if InStr = $ESC THEN EXIT DO
                LOOP
                Dale

                Comment


                • #9
                  Time for one of my favorite sayings.

                  Computers don't do the math. People do the math.
                  Computers do the arithmetic.

                  The Math here is to simply solve the simutaneous equations

                  Y1 = mX1 + c
                  Y2 = mX2 + c

                  .. for m and c.


                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Here ya go.

                    Note this determines if Point 3 lies on the line through Points 1 and 2 (i.e. the line described by the equation), not the line between points 1 and 2.
                    If you require the latter you would need to also check whether x3 lies within x1 and x2 and whether y3 lies within y1 and y2!


                    '
                    Code:
                    #COMPILE EXE
                    FUNCTION PBMAIN() AS LONG
                        LOCAL x1,x2,x3,y1,y2,y3,a,b AS DOUBLE
                        LOCAL wstrF, wstrL AS WSTRING
                        'Line points
                        x1 = 1 : y1 = 2
                        x2 = 2 : y2 = 4
                        'Test point
                        x3 = 3 : y3 = 6
                    
                        wstrF = "The equation for the line (" & STR$(x1,4) & " , " & STR$(y1,4) & ") - (" & STR$(x2,4) & " , " & STR$(y2,4) & ") is:" & $LF
                        wstrL =  $LF & "The point (" & STR$(x3,4) & " ," & STR$(y3,4) & ") is "
                        IF x1 = x2 THEN 'line is vertical
                          wstrF &= "X = " & STR$(x1,3)
                          IF x3 = x1 THEN 'point is on line
                             wstrL &= " is on the line"
                          ELSE
                             wstrl &= " is NOT on the line"
                          END IF
                        ELSE
                            wstrF &= "Y="
                            IF y1 = y2 THEN ' line is horizontal
                                wstrF &=  "0x + " & STR$(y1,4)
                                IF y3 = y1 THEN ' point is on line
                                    wstrL &= " is on the line"
                                ELSE
                                    wstrl &= " is NOT on the line"
                                END IF
                            ELSE ' line is neither horizontaol nor vertical
                                a = (y2-y1)/(x2-x1)
                                b = (x2*y1-x1*y2)/(x2-x1)
                                wstrF &= STR$(a,4) & "X " & IIF$(b>=0," +"," ") &  STR$(b,3)
                                IF   y3 = a*x3 + b  THEN
                                    wstrL &= " is on the line"
                                ELSE
                                    wstrl &= " is NOT on the line"
                                END IF
                    
                            END IF
                        END IF
                        ? wstrF & wstrL
                    END FUNCTION
                    '

                    Comment


                    • #11
                      Originally posted by Michael Mattias View Post
                      Time for one of my favorite sayings.

                      Computers don't do the math. People do the math.
                      Computers do the arithmetic.
                      You mean SIN(),COS(),TAN(),ATAN(),SQR(),EXP,LOG,MAT etc are just arithmetic?

                      Comment


                      • #12
                        Thank you Sir Stuart, I will try to do the check of x3 is between x1 and x2 as well as y3 is between x1 and x2 to see if I can do the math!

                        Comment


                        • #13
                          maybe like this
                          Code:
                          #COMPILE EXE
                          #DIM ALL
                          
                          FUNCTION PBMAIN() AS LONG
                              LOCAL x1,x2,x3,y1,y2,y3,a,b AS DOUBLE
                              LOCAL wstrF, wstrL , wstrBet AS WSTRING
                              LOCAL FlagbetX , FlagbetY AS LONG
                          
                              'Set 1
                              'Line points
                             ' x1 = 1 : y1 = 2
                             ' x2 = 4 : y2 = 8
                              'Test point
                             ' x3 = 3 : y3 = 6
                          
                              'Set 2  -- answer check correct  y = -3x -5  test point is NOT on line
                              ' https://content.byui.edu/file/b8b83119-9acc-4a7b-bc84-efacf9043998/1/Math-2-11-2.html#WS1
                              'Line points
                             ' x1 = -5 : y1 = 10
                            '  x2 = -3 : y2 = 4
                              'Test point
                            '  x3 = -2 : y3 = 7
                          
                             'Set 3  -- answer check correct  y = 6x +4  test point is on line
                              ' https://content.byui.edu/file/b8b83119-9acc-4a7b-bc84-efacf9043998/1/Math-2-11-2.html#WS1
                              'Line points
                             ' x1 = -5 : y1 = -26
                             ' x2 = -2 : y2 = -8
                              'Test point
                             ' x3 = -20 : y3 = -116
                          
                              'Set 4  -- answer check correct  y = 6x +4  test point is on line
                              ' https://content.byui.edu/file/b8b83119-9acc-4a7b-bc84-efacf9043998/1/Math-2-11-2.html#WS1
                              'Line points
                              x1 = -5 : y1 = -26
                              x2 = -2 : y2 = -8
                              'Test point
                              x3 = -3 : y3 = -14
                          
                              wstrF = "The equation for the line (" & STR$(x1,4) & " , " & STR$(y1,4) & ") - (" & STR$(x2,4) & " , " & STR$(y2,4) & ") is:" & $LF
                              wstrL =  $LF & "The point (" & STR$(x3,4) & " ," & STR$(y3,4) & ") is "
                              IF x1 = x2 THEN 'line is vertical
                                wstrF &= "X = " & STR$(x1,3)
                                IF x3 = x1 THEN 'point is on line
                                   wstrL &= " is on the line"
                                ELSE
                                   wstrl &= " is NOT on the line"
                                END IF
                              ELSE
                                  wstrF &= "Y="
                                  IF y1 = y2 THEN ' line is horizontal
                                      wstrF &=  "0x + " & STR$(y1,4)
                                      IF y3 = y1 THEN
                                           ' point is on line
                                          wstrL &= " is on the line"
                                      ELSE
                                          wstrl &= " is NOT on the line"
                                      END IF
                                  ELSE ' line is neither horizontaol nor vertical
                                      a = (y2-y1)/(x2-x1)
                                      b = (x2*y1-x1*y2)/(x2-x1)
                                      wstrF &= STR$(a,4) & "X " & IIF$(b>=0," +"," ") &  STR$(b,3)
                                      IF   y3 = a*x3 + b  THEN
                                          wstrL &= " is on the line"
                                       ' check if x3 is between x1 and x2
                                         FlagbetX = 0
                                         IF x2 >= x1 THEN
                                           IF x3 >= x1 THEN
                                           IF x3 <= x2 THEN
                                             FlagbetX = 1
                                           END IF
                                           END IF
                                         ELSE
                                           IF x3 >= x2 THEN
                                           IF x3 <= x1 THEN
                                             FlagbetX = 1
                                           END IF
                                           END IF
                                         END IF
                                         ' check if y3 is between y1 and y2
                                         FlagbetY = 0
                                         IF y2 >= y1 THEN
                                            IF y3 >= y1 THEN
                                            IF y3 <= y2 THEN
                                               FlagbetY = 1
                                            END IF
                                            END IF
                                         ELSE
                                            IF y3 >= y2 THEN
                                            IF y3 <= y1 THEN
                                               FlagbetY = 1
                                            END IF
                                            END IF
                                         END IF
                          
                                         wstrBet = $CRLF +"But it does NOT lies between the given Line points"
                                         IF FlagbetX + FlagbetY = 2 THEN
                                            wstrBet = $CRLF + "And it also lies between the given Line points"
                                         END IF
                          
                                      ELSE
                                          wstrl &= " is NOT on the line"
                                      END IF
                          
                                  END IF
                              END IF
                              ? wstrF & wstrL  & wstrBet
                          END FUNCTION

                          Comment


                          • #14
                            Another way without all of those IFs.
                            '
                            Code:
                               FlagbetX = (x3 <= MAX(x1,x2)) AND (x3 >= MIN(x1,x2))
                               FlagbetY = (y3 <= MAX(y1,y2)) AND (y3 >= MIN(y1,y2))
                            
                               IF NOT FlagbetX THEN wstrL &= $LF & "X = " & STR$(x3,4) & " is outside of the X bounds of the line"
                               IF NOT FlagbetY THEN wstrL &= $LF & "Y = " & STR$(y3,4) & " is outside of the Y bounds of the line"
                            '

                            Comment


                            • #15
                              Originally posted by Mike Doty View Post
                              Mark,
                              GoTo GRD not found
                              Thanks, I added it in an edit.
                              Politically incorrect signatures about immigration patriots are forbidden. Searching “immigration patriots” is forbidden. Thinking about searching ... well, don’t even think about it.

                              Comment


                              • #16
                                finally figured out the graphcs too

                                Click image for larger version

Name:	linpt.png
Views:	180
Size:	26.6 KB
ID:	816438

                                Comment


                                • #17
                                  better graphics scaling ..
                                  Code:
                                    #COMPILE EXE
                                  #DIM ALL
                                  
                                  FUNCTION PBMAIN() AS LONG
                                      LOCAL x1,x2,x3,y1,y2,y3,a,b AS DOUBLE
                                      LOCAL wstrF, wstrL , wstrBet AS WSTRING
                                      LOCAL FlagbetX , FlagbetY AS LONG
                                  
                                      'Set 1  -- y = 2x where  test point is on the line and lies between
                                      ' the line points
                                      'Line points
                                    '  x2 = 1 : y2 = 2
                                     ' x1 = 4 : y1 = 8
                                      'Test point
                                     ' x3 = 3 : y3 = 6
                                  
                                      'Set 2  -- answer check correct  y = -3x -5  test point is NOT on line
                                      ' https://content.byui.edu/file/b8b83119-9acc-4a7b-bc84-efacf9043998/1/Math-2-11-2.html#WS1
                                      'Line points
                                     ' x1 = -5 : y1 = 10
                                     ' x2 = -3 : y2 = 4
                                      'Test point
                                      ' x3 = -2 : y3 = 7
                                  
                                  
                                     'Set 3  -- answer check correct  y = 6x +4  test point is on line but not between line points
                                      ' https://content.byui.edu/file/b8b83119-9acc-4a7b-bc84-efacf9043998/1/Math-2-11-2.html#WS1
                                      'Line points
                                     ' x2 = -5 : y2 = -26
                                     ' x1 = -2 : y1 = -8
                                      'Test point
                                     ' x3 = -7 : y3 = -38
                                  
                                      'Set 4  -- answer check correct  y = 6x +4  test point is on line and lies between
                                      ' the line points
                                      ' https://content.byui.edu/file/b8b83119-9acc-4a7b-bc84-efacf9043998/1/Math-2-11-2.html#WS1
                                      'Line points
                                     ' x2 = -5 : y2 = -26
                                     ' x1 = -2 : y1 = -8
                                      'Test point
                                     ' x3 = -3 : y3 = -14
                                  
                                  
                                      'Set 5  -- answer check correct  y = 4.5x - 24  test point is NOT on the line
                                      ' https://content.byui.edu/file/b8b83119-9acc-4a7b-bc84-efacf9043998/1/Math-2-11-2.html#WS1
                                      'Line points
                                     ' x2 = 6 : y2 = 3
                                     ' x1 = 4 : y1 = -6
                                      'Test point
                                     ' x3 = 3.6 : y3 = -10
                                  
                                  
                                        'Set 6  -- answer check correct  y = 1.5x - 2.5  test point is NOT on the line
                                      ' https://content.byui.edu/file/b8b83119-9acc-4a7b-bc84-efacf9043998/1/Math-2-11-2.html#WS1
                                      'Line points
                                      x2 = 3 : y2 = 2
                                      x1 = 5 : y1 = 5
                                      'Test point
                                      x3 = 4 : y3 = 4
                                  
                                  
                                  
                                      wstrF = "The equation for the line (" & STR$(x1,4) & " , " & _
                                               STR$(y1,4) & ") - (" & STR$(x2,4) & " , " & STR$(y2,4) & ") is:" & $LF
                                      wstrL =  $LF & "The point (" & STR$(x3,4) & " ," & STR$(y3,4) & ")  "
                                      IF x1 = x2 THEN
                                          'line is vertical
                                        wstrF &= "X = " & STR$(x1,3)
                                        IF x3 = x1 THEN
                                             'point is on line
                                           wstrL &= " is on the line"
                                        ELSE
                                           wstrl &= " is NOT on the line"
                                        END IF
                                      ELSE
                                          wstrF &= "Y="
                                          IF y1 = y2 THEN
                                               ' line is horizontal
                                              wstrF &=  "0x + " & STR$(y1,4)
                                              IF y3 = y1 THEN
                                                   ' point is on line
                                                  wstrL &= " is on the line"
                                              ELSE
                                                  wstrl &= " is NOT on the line"
                                              END IF
                                          ELSE
                                              ' line is neither horizontaol nor vertical
                                              a = (y2-y1)/(x2-x1)
                                              b = (x2*y1-x1*y2)/(x2-x1)
                                              wstrF &= STR$(a,4) & "X " & IIF$(b>=0," +"," ") &  STR$(b,3)
                                              IF   y3 = a*x3 + b  THEN
                                                  wstrL &= " is on the line"
                                               ' check if x3 is between x1 and x2
                                                 FlagbetX = 0
                                                 IF x2 >= x1 THEN
                                                   IF x3 >= x1 THEN
                                                   IF x3 <= x2 THEN
                                                     FlagbetX = 1
                                                   END IF
                                                   END IF
                                                 ELSE
                                                   IF x3 >= x2 THEN
                                                   IF x3 <= x1 THEN
                                                     FlagbetX = 1
                                                   END IF
                                                   END IF
                                                 END IF
                                                 ' check if y3 is between y1 and y2
                                                 FlagbetY = 0
                                                 IF y2 >= y1 THEN
                                                    IF y3 >= y1 THEN
                                                    IF y3 <= y2 THEN
                                                       FlagbetY = 1
                                                    END IF
                                                    END IF
                                                 ELSE
                                                    IF y3 >= y2 THEN
                                                    IF y3 <= y1 THEN
                                                       FlagbetY = 1
                                                    END IF
                                                    END IF
                                                 END IF
                                  
                                                 wstrBet = $CRLF +"But it does NOT lies between the given Line points"
                                                 IF FlagbetX + FlagbetY = 2 THEN
                                                    wstrBet = $CRLF + "And it also lies between the given Line points"
                                                 END IF
                                  
                                              ELSE
                                                  wstrl &= " is NOT on the line"
                                              END IF
                                  
                                          END IF
                                      END IF
                                  
                                  
                                  
                                    ' draw the graphics
                                   ' pirated from
                                   '  https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/816259-how-to-draw-a-polygon-graphically?p=816366#post816366
                                  
                                  
                                      LOCAL hGraph AS DWORD
                                      LOCAL xmin,xmax ,ymin, ymax AS DOUBLE
                                  
                                    ' obtain the  min and max coord of the 3 points
                                    ' to do the scaling
                                      xmin = MIN(x1,x2,x3)
                                      ymin = MIN(y1,y2,y3)
                                      xmax = MAX(x1,x2,x3)
                                      ymax = MAX(y1,y2,y3)
                                  
                                      GRAPHIC WINDOW NEW "Line and points",100,100,400,400 TO hGraph
                                    ' Scale Windows so that points coordinates fit nicely
                                      GRAPHIC SCALE (xmin-5, ymax+5) - (xmax+5, ymin-5)
                                      GRAPHIC COLOR %RGB_RED
                                  
                                    'Draw the line
                                     GRAPHIC LINE (x1,y1) - (x2,y2)
                                  
                                   ' draw the points in large dots
                                     GRAPHIC BOX (x1-.2,y1-.2) - _
                                                 (x1+.2, y1+.2),100,%RGB_BLUE,%RGB_BLUE
                                     GRAPHIC BOX (x2-.2,y2-.2) - _
                                                 (x2+.2, y2+.2),100,%RGB_BLUE,%RGB_BLUE
                                  
                                     GRAPHIC BOX (x3-.2,y3-.2) - _
                                                 (x3+.2, y3+.2),100,%RGB_LIME,%RGB_LIME
                                  
                                       ' display messagebox
                                       ? wstrF & wstrL  & wstrBet
                                  
                                      GRAPHIC WAITKEY$
                                    GRAPHIC WINDOW END hGraph
                                  
                                  END FUNCTION

                                  Comment


                                  • #18
                                    Mark,

                                    In your edit you did not add REDRAW option to GRAPHIC ATTACH, nor remove the GRAPHIC REDRAW line.

                                    Without the option, GRAPHIC REDRAW has no purpose.
                                    Dale

                                    Comment


                                    • #19
                                      Lines like this:
                                      Code:
                                      IF y3 = a*x3 + b THEN
                                      are going to cause trouble with real-world data that may have been rounded. Instead, try something like:
                                      Code:
                                      IF ABS(y3 - (a*x3 +b)) < SmallNumber THEN
                                      (where SmallNumber is previously set to a small positive value).
                                      Dan

                                      Comment


                                      • #20
                                        You mean SIN(),COS(),TAN(),ATAN(),SQR(),EXP,LOG,MAT etc are just arithmetic?
                                        Not that those functions are relevant here but you have posed an interesting question.

                                        SIN(), COS() and TAN() are ratios. SQR() and EXP() are logarithmic ( SQR(X) = EXP(LOG(X)/2). MAT() I think is 'math' or maybe it's just a method (Not a COM method!) of treating sets. I don't know what I would label MAT()

                                        You know, I'm not sure where values in the "trig tables" come from. I've just of always treated them as being fact. Hell, they work here.

                                        Michael Mattias
                                        Tal Systems (retired)
                                        Port Washington WI USA
                                        [email protected]
                                        http://www.talsystems.com

                                        Comment

                                        Working...
                                        X