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
    Thanks for the help. I didn't know because, like, Euclid retired before I even started school.

    Leave a comment:


  • Kurt Kuzba
    replied
    Originally posted by Michael Mattias View Post
    No, no, before that. Like, how the heck did Euclid get them?
    It's absolutely simple.
    You look at the thing, and you say to yourself that there must be a fixed relationship between two aspects.
    Then you look for that relationship. Eventually, you are able to obtain enough data from observations.
    Then you are able to construct a formal expression of the relationship, which you then test.
    I can get the first part. The rest is Greek.

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Michael Mattias View Post
    No, no, before that. Like, how the heck did Euclid get them?
    Euclid din't know about sines etc. You are confusing trigonomentry with geometry (the axioms of which Euclid did develop). SInes etc didn't come into use until the Indian and Arabic mathematicians derived the concepts many centuries later.

    It's a long story. Early trigonometry came a few centuries after Euclid and started with the chord - the arc subtended by an angle and the ratio of the chord to the radius. That one is easly to derive since 1° is 1/360th of the circumference. It is what thet Greeks such as Ptolemy used for astronomical claculations.

    Leave a comment:


  • Michael Mattias
    replied
    At a guess, I'd say "trig tables" come from Trigonometry
    Computers generall calculate them, using the CORDIC algorithm.
    No, no, before that. Like, how the heck did Euclid get them?

    Leave a comment:


  • Tim Lakinir
    replied
    here's the graphics for data set # 1


    Click image for larger version  Name:	point line.png Views:	0 Size:	46.8 KB ID:	816455

    Leave a comment:


  • Tim Lakinir
    replied
    the below codes with some graphics inclusive of X and Y axes, please comment on it and enhance it
    so that a better program emerges. Noteworthy I have pirated some codes from Stuart and Dave Biggs as well as improving it with Dan Soper's suggestion. Thanks everyone on this

    There are 10 data sets to test these codes and please uncomment each set to test it as well
    as displaying its graphics

    Code:
    ' Point on Line.bas
    ' https://forum.powerbasic.com/forum/user-to-user-discussions/programming/816417-need-codes-to-find-equation-of-straight-line-y-mx-b?p=816429#post816429
    ' Obtain the equation of a line given 2 points (x1,y1) and (x2,y2)
    ' check whether point (x3,y3) lies on this line
    ' Great Thanks to Stuart , Dave Biggs
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN() AS LONG
        LOCAL x1,x2,x3,y1,y2,y3,gradM,IntC AS DOUBLE
        LOCAL wstrF, wstrL , wstrBet AS WSTRING
        LOCAL FlagbetX , FlagbetY AS LONG
        LOCAL setNumSt AS STRING
    
        '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
       ' setNumSt = " 1"
    
        '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
        '  setNumSt = " 2"
    
       '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
        ' setNumSt = " 3"
    
    
    
        '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
      '  setNumSt = " 4"
    
    
        '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
        ' setNumSt = " 5"
    
    
          '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
       ' setNumSt = " 6"
    
    
        ' Set 7  -- horizontal line  y = 2
         'Line points
       ' x2 = 3 : y2 = 2
       ' x1 = 5 : y1 = 2
        'Test point
       ' x3 = 4 : y3 = 3
        ' setNumSt = " 7"
    
          ' Set 7B  -- horizontal line  y = 2
         'Line points
       ' x2 = 3 : y2 = 2
       ' x1 = 5 : y1 = 2
        'Test point
       ' x3 = 4 : y3 = 2
       ' setNumSt = " 7B"
    
    
          'Set 8  -- Vertical line   X = 2
        'Line points
        'x2 = 2 : y2 = 3
       ' x1 = 2 : y1 = -6
        'Test point
       ' x3 = 2 : y3 = 3
       ' setNumSt = " 8"
    
    
        'Set 8B  -- Vertical line   X = 2
        'Line points
        x2 = 2 : y2 = 3
        x1 = 2 : y1 = -6
        'Test point
        x3 = 1.5 : y3 = -1.8
        setNumSt = " 8B"
    
    
    
        ' check that all coordinates are valid
         IF x1 = 0 AND y1 = 0 THEN
            IF x2 = 0 AND y2 = 0 THEN
                ' all coord are Invalid
                ? " invalid coordinates "
                EXIT FUNCTION
            END IF
         END IF
         IF x1 = x2 AND y1 = y2 THEN
              ' all coord are Invalid
                ? " invalid coordinates "
                EXIT FUNCTION
         END IF
    
    
        wstrF = "The equation for the line" & $LF & _
                "(" & STR$(x1,4) & " , " & STR$(y1,4) & ") - (" & _
                 STR$(x2,4) & " , " & STR$(y2,4) & ")  is :" & $LF
        wstrL =  $LF & $LF & "The point (" & STR$(x3,4) & " ," & STR$(y3,4) & ")  "
    
        LOCAL smallNum AS DOUBLE
        smallNum = 0.0001
    
        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 &=   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 horizontal nor vertical
                gradM = (y2-y1)/(x2-x1)
                IntC = (x2*y1-x1*y2)/(x2-x1)
                IF  ABS(gradM)= 0 THEN
                   wstrF &=   STR$(IntC,3)
                ELSE
                   wstrF &= STR$(gradM,4) & "X " & IIF$(IntC>=0," +"," ") &  STR$(IntC,3)
                END IF
    
              ' Thanks to Dan to use a smallNum check
              ' https://forum.powerbasic.com/forum/user-to-user-discussions/programming/816417-need-codes-to-find-equation-of-straight-line-y-mx-b?p=816444#post816444
                IF  ABS(y3 - (gradM*x3 + IntC)) <= smallNum  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
        LOCAL xmark, ymark AS DOUBLE
       ' marks per axis
        LOCAL NumStep AS LONG
       ' canvas margins
        LOCAL lm, tm, rm, bm AS DOUBLE
       ' scaled margins
        LOCAL slm, stm, srm, sbm AS DOUBLE
      ' scaled xy coordinates for circular dots
        LOCAL scx , scy , xrat , yrat AS DOUBLE
    
    
       LOCAL  TitleLab, XaxisLab, YaxisLab AS STRING
       LOCAL  hFont1, hFontR AS DWORD
    
       ' set up fonts
       FONT NEW "Lucida Console", 10, 0, 0, 0, 0  TO hFont1
       ' rotated font (needs to be true type)
       FONT NEW "Lucida Console", 10, 0, 0, 0, 900 TO hFontR
    
    
    
      ' 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,500,500 TO hGraph
        GRAPHIC ATTACH hGraph, 0
       'white background
        GRAPHIC CLEAR %WHITE
    
      ' Left , Right, Top and Bottom Canvas Margins (in pixels)
      ' adjust these to the canvas window size
       ' lm = 80 : rm = 60 : tm = 70 : bm = 90
         lm = 130 : rm = 60 : tm = 90 : bm = 140
    
    
    
        ' Calculate the scaled margins
        slm = lm * (xmax-xmin)/(GRAPHIC(CANVAS.X) - lm - rm)
        srm = rm * (xmax-xmin)/(GRAPHIC(CANVAS.X) - lm - rm)
        stm = tm * (ymax-ymin)/(GRAPHIC(CANVAS.Y) - tm - bm)
        sbm = bm * (ymax-ymin)/(GRAPHIC(CANVAS.Y) - tm - bm)
    
      ' adjustments if scaled margins are zero
        IF slm = 0 THEN
            slm =   lm * (xmax -xmax*0.5)/(GRAPHIC(CANVAS.X) - lm - rm)
        END IF
        IF srm = 0 THEN
            srm =   rm * (xmax-xmax*0.5)/(GRAPHIC(CANVAS.X) - lm - rm)
        END IF
        IF stm = 0 THEN
           stm = tm * (ymax-ymax*0.5)/(GRAPHIC(CANVAS.Y) - tm - bm)
        END IF
        IF sbm = 0 THEN
          sbm = bm * (ymax-ymax*0.5)/(GRAPHIC(CANVAS.Y) - tm - bm)
        END IF
    
              '  ? STR$(slm) +  "  " + STR$(srm) +  "  " + STR$(stm) +  "  " + STR$(sbm)
    
    
    
        ' x , y ratios of canvas
         xrat = (xmax-xmin)/GRAPHIC(CANVAS.X)
         yrat = (ymax-ymin)/GRAPHIC(CANVAS.Y)
         IF xrat = 0 THEN
            xrat = (xmax-xmax*0.5)/GRAPHIC(CANVAS.X)
         END IF
         IF yrat = 0 THEN
            yrat = (ymax-ymax*0.5)/GRAPHIC(CANVAS.Y)
         END IF
    
    
       ' calculate the size of the circular dot for the points
       ' adjust the multiplier to suit size of dot
         scx = 7 * xrat
         scy = 7 * yrat
    
      ' Scale Windows so that points coordinates fit nicely
        GRAPHIC SCALE (xmin-slm, ymax+stm) - (xmax+srm, ymin-sbm)
    
    
      ' Line is in Lime over a white background
        GRAPHIC WIDTH 1
        GRAPHIC COLOR %RGB_LIME, %WHITE
      ' Draw the line
        GRAPHIC LINE (x1,y1) - (x2,y2)
    
    
     ' draw the points in large dots
       GRAPHIC BOX (x1-scx,y1-scy) - _
                   (x1+scx, y1+scy),100,%RGB_BLUE,%RGB_BLUE
       GRAPHIC BOX (x2-scx,y2-scy) - _
                   (x2+scx, y2+scy),100,%RGB_BLUE,%RGB_BLUE
    
     ' the third point in Magenta
       GRAPHIC BOX (x3-scx, y3-scy) - _
                   (x3+scx, y3+scy),100,%RGB_MAGENTA,%RGB_MAGENTA
    
    
    
    
      ' draw the axes -------------------------------------------------------
      ' calculate axes mark spacing based on data range / values
        NumStep = 10
        IF xmax = xmin THEN
           xmark = CalculateStepSize(xmax*0.5, xmax, NumStep)
        ELSE
           xmark = CalculateStepSize(xmin, xmax, NumStep)
        END IF
        IF ymax = ymin THEN
           ymark = CalculateStepSize( ymax*0.5, ymax, NumStep)
        ELSE
           ymark = CalculateStepSize(ymin, ymax, NumStep)
        END IF
    
      ' Draw the axes labels
        GRAPHIC COLOR %RGB_MEDIUMBLUE, %WHITE
        TitleLab = "Plot for data set #" + setNumSt
        GRAPHIC SET FONT hFont1
        GRAPHIC SET POS (xmax-(xmax-xmin)/2 - _
            GRAPHIC(TEXT.SIZE.X, TitleLab)/2, ymax + stm*0.4)
        GRAPHIC PRINT TitleLab
    
        ' Display the x axis label
         XaxisLab = "X-Axis"
        GRAPHIC SET POS (xmax-(xmax-xmin)/2 - _
            GRAPHIC(TEXT.SIZE.X, XaxisLab)/2, ymin - sbm*0.45)
        GRAPHIC PRINT XaxisLab
    
         ' Draws the x axis line
        GRAPHIC WIDTH 2
        GRAPHIC COLOR %RGB_BLACK, %WHITE
        GRAPHIC LINE (xmin, ymin) - (xmax, ymin)
        GRAPHIC WIDTH 1
    
       ' draw marks on the x axis
        LOCAL nchar AS LONG
    
        LOCAL rr AS DOUBLE
        LOCAL km AS LONG
        km = 0
        FOR rr = xmin TO xmax STEP xmark
          INCR km
          GRAPHIC LINE (rr, ymin) - (rr, ymin-GRAPHIC(CELL.SIZE.Y)*0.75)
          ' Display the mark values
          nchar =LEN( TRIM$(STR$(rr),ANY CHR$(0) + CHR$(32)+ CHR$(10) + CHR$(13)+ CHR$(9)))
          IF nchar = 1 THEN
             GRAPHIC SET POS STEP (-GRAPHIC(CELL.SIZE.X)*0.55, - GRAPHIC(CELL.SIZE.Y)*0.5)
          ELSEIF nchar = 2 THEN
             GRAPHIC SET POS STEP (-GRAPHIC(CELL.SIZE.X)*0.9, - GRAPHIC(CELL.SIZE.Y)*0.5)
          ELSEIF nchar = 3 THEN
             GRAPHIC SET POS STEP (-GRAPHIC(CELL.SIZE.X)*1.5, - GRAPHIC(CELL.SIZE.Y)*0.5)
          ELSE
             GRAPHIC SET POS STEP (-GRAPHIC(CELL.SIZE.X)*1.8, - GRAPHIC(CELL.SIZE.Y)*0.5)
          END IF
    
          ' print the scale values on x axis
          ' (alternate values only to prevent clutter)
          IF km MOD 2 = 0 THEN
             IF xmark < .1 THEN
                GRAPHIC PRINT TRIM$(FORMAT$(rr, "0.00"))
             ELSEIF xmark < 1 THEN
                GRAPHIC PRINT TRIM$(FORMAT$(rr, "0.0"))
             ELSE
               GRAPHIC PRINT TRIM$(rr)
            END IF
          END IF
        NEXT rr
    
    
       ' Display the y axis label
       ' Using rotated font at 90deg
        YaxisLab = "Y-Axis"
        GRAPHIC SET FONT hFontR
        GRAPHIC SET POS (xmin-slm*.7, (ymax-(ymax-ymin)/2) _
           - GRAPHIC(TEXT.SIZE.X, YaxisLab)/2)
        GRAPHIC PRINT YaxisLab
      ' back to font1
        GRAPHIC SET FONT hFont1
    
        ' Draw the y axis line
        GRAPHIC WIDTH 2
        GRAPHIC COLOR %RGB_BLACK, %WHITE
        GRAPHIC LINE (xmin, ymin) - (xmin, ymax)
        GRAPHIC WIDTH 1
    
         ' draw marks on the Y axis
        LOCAL tmpSt AS STRING
        km = 0
        FOR rr = ymin TO ymax STEP ymark
            INCR km
          GRAPHIC LINE (xmin, rr) - (xmin-GRAPHIC(CELL.SIZE.X), rr)
         ' Display the mark values
          IF rr < .1 THEN
             tmpSt = TRIM$(FORMAT$(rr, "0.00"))
          ELSEIF rr < 1 THEN
             tmpSt = TRIM$(FORMAT$(rr, "0.0"))
          ELSEIF rr = 0 THEN
             tmpSt = TRIM$(STR$(rr))
          ELSE
             tmpSt = TRIM$(STR$(rr))
          END IF
          nchar =LEN(TRIM$(tmpSt,ANY CHR$(0) + CHR$(32)+ CHR$(10) + CHR$(13)+ CHR$(9)))
          IF rr = 0 THEN
              nchar = 1
          END IF
          IF nchar <= 2 THEN
             GRAPHIC SET POS STEP (-GRAPHIC(CELL.SIZE.X)*3, GRAPHIC(CELL.SIZE.Y)/2)
          ELSEIF nchar = 3 THEN
             GRAPHIC SET POS STEP (-GRAPHIC(CELL.SIZE.X)*4.2, GRAPHIC(CELL.SIZE.Y)/2)
          ELSEIF nchar >= 4 THEN
             GRAPHIC SET POS STEP (-GRAPHIC(CELL.SIZE.X)*4.5, GRAPHIC(CELL.SIZE.Y)/2)
          END IF
          ' print scale values -- alternate values only
          ' to prevent cluther
          IF km MOD 2 = 0 THEN
             IF ymark < .1 THEN
               GRAPHIC PRINT TRIM$(FORMAT$(rr, "0.00"))
             ELSEIF ymark < 1 THEN
                GRAPHIC PRINT TRIM$(FORMAT$(rr, "0.0"))
             ELSE
                GRAPHIC PRINT TRIM$(rr)
             END IF
          END IF
        NEXT rr
    
    
    
    
    
    
         ' display the messagebox
         ? wstrF & wstrL  & wstrBet
    
        GRAPHIC WAITKEY$
      GRAPHIC WINDOW END hGraph
    
    END FUNCTION
    
    
    
    ' Thanks to Dave Biggs
     ' Compute the  marks positions on the axes
    ' Approximately there are around 10 marks on each axes
    FUNCTION CalculateStepSize(dmin AS DOUBLE, dmax AS DOUBLE, NStep AS LONG) AS DOUBLE
     LOCAL dRange, tempstep, StepSize AS DOUBLE
     LOCAL mag, magPow, magMsd, mult AS LONG
    
     ' NStep = 10           ' eg maximum marks on axis
      dRange = dmax -dmin
      IF dRange = 0 THEN
           EXIT FUNCTION
      END IF
    
      ' calculate an initial guess at step size
      tempStep = dRange/NStep
      ' scale up low magnitude step sizes (avoid negative log10 values)
      WHILE tempstep < 1
        TempStep = TempStep * 10
        INCR mult
      WEND
      ' get the magnitude of the step size
      mag = FIX(LOG10(tempStep))
      magPow = 10^mag
      ' calculate most significant digit of the new step size
      magMsd = ((tempStep/magPow) + 0.5)
      ' promote the MSD TO either 1, 2, OR 5
        IF (magMsd > 5.0) THEN
          magMsd = 10.0
        ELSEIF (magMsd > 2.0) THEN
          magMsd = 5.0
        ELSEIF (magMsd > 1.0) THEN
          magMsd = 2.0
        END IF
      StepSize = magMsd*magPow
      ' reduce scaled up magnitudes if required
      StepSize = StepSize / 10^mult
      ' Adjust _min and _max
      dmin = StepSize * INT(dmin/StepSize)
      dmax = StepSize * CEIL(dmax/StepSize)
     FUNCTION = StepSize
    END FUNCTION

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Mark Hunter View Post
    ...
    To go off on a tangent, ...
    I saw what you did there!


    Leave a comment:


  • Mark Hunter
    replied
    Originally posted by Michael Mattias View Post
    SQR(X) = EXP(LOG(X)/2)
    The formula is true, and its generalization may be used for other fractional powers, X ^ s = Exp(s * Log(X)), but I doubt it's used for the special case s = 1 / 2. That case is so common (geometry, statistics, physics, engineering) I imagine it would be especially optimized.

    To go off on a tangent, X ^ n is X times itself n times. Call that the third operation, the first two operations being addition and multiplication. Multiplication is repeated addition and exponential is repeated multiplication. You can imagine a fourth operation which is repeated exponentiation, and ask if it could defined for fractional n as it can be for multiplication and exponentiation. An Internet search on
    fourth operation
    discovers an article about it – Bing #1, DDG #1, Google #8 (the rankings vary a bit over time).
    Last edited by Mark Hunter; 12 May 2022, 12:22 AM.

    Leave a comment:


  • Stuart McLachlan
    replied
    Originally posted by Michael Mattias View Post
    ...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.
    Mat() operations are generally classed as "matrix Algebra".

    At a guess, I'd say "trig tables" come from Trigonometry
    Computers generall calculate them, using the CORDIC algorithm.

    Algebra and Trigonomentry, like Arithmetic are branches of Mathematics.


    Leave a comment:


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

    Leave a comment:


  • Dan Soper
    replied
    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).

    Leave a comment:


  • Dale Yarker
    replied
    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.

    Leave a comment:


  • Tim Lakinir
    replied
    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

    Leave a comment:


  • Tim Lakinir
    replied
    finally figured out the graphcs too

    Click image for larger version

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

    Leave a comment:


  • Mark Hunter
    replied
    Originally posted by Mike Doty View Post
    Mark,
    GoTo GRD not found
    Thanks, I added it in an edit.

    Leave a comment:


  • Stuart McLachlan
    replied
    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"
    '

    Leave a comment:


  • Tim Lakinir
    replied
    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

    Leave a comment:


  • Tim Lakinir
    replied
    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!

    Leave a comment:


  • Stuart McLachlan
    replied
    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?

    Leave a comment:


  • Stuart McLachlan
    replied
    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
    '

    Leave a comment:

Working...
X