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

  • Michael Mattias
    replied
    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.


    Leave a comment:


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

    Leave a comment:


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

    Leave a comment:


  • Dale Yarker
    replied
    Why GRAPHIC REDRAW? GRAPHIC ATTACH does not have REDRAW option.

    Cheers,

    Leave a comment:


  • Mike Doty
    replied
    Mark,
    GoTo GRD not found

    Leave a comment:


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

    Leave a comment:


  • Mark Hunter
    replied
    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

    Leave a comment:


  • Gary Beene
    replied
    Howdy, Tim!

    This page explains the steps ...


    Leave a comment:


  • 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.
Working...
X