Author Topic: Generate random quadratic equations and plot them  (Read 3242 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Generate random quadratic equations and plot them
« on: November 14, 2020, 03:23:55 pm »
Polished up ancient CGA code because I saw the word "quadratic" on
Discord, and don't feel like doing real work!

Code: QB64: [Select]
  1. DEFINT A-Z
  2.  
  3. DECLARE FUNCTION Eq! (x!)
  4. DECLARE SUB Wubba ()
  5.  
  6. COMMON SHARED minx!, miny!, ma, xx!, maxy!, r1!, r2!, vx!, vy!, sx!, sy!, s!
  7.  
  8. DIM SHARED a, b, c
  9. DIM SHARED c(4), c$(4), r$(30), white
  10.  
  11. white = _RGB32(222, 222, 222)
  12.  
  13. SCREEN _NEWIMAGE(800, 600, 32)
  14.  
  15. Begin:
  16. FOR i = 1 TO 4
  17.     c(i) = RND * 9 + 1
  18.     IF (RND * 9) > 5 THEN c(i) = -c(i)
  19.     'c(1) = -1: c(2) = 1: c(3) = -1: c(4) = 3
  20.     c$(i) = LTRIM$(STR$(c(i)))
  21.     IF i < 3 THEN
  22.         IF c(i) = 1 THEN c$(i) = ""
  23.         IF c(i) = -1 THEN c$(i) = "-"
  24.     ELSE
  25.         IF c(i) > 0 THEN c$(i) = "+" + c$(i)
  26.     END IF
  27. s1$ = "(" + c$(1) + "x" + c$(3) + ")"
  28. s2$ = "(" + c$(2) + "x" + c$(4) + ")"
  29. s$ = s1$ + " " + s2$
  30. x2 = c(1) * c(2)
  31. x2$ = LTRIM$(STR$(x2))
  32. IF x2$ = "1" THEN x2$ = ""
  33. IF x2$ = "-1" THEN x2$ = "-"
  34. x1 = c(1) * c(4) + c(2) * c(3): IF x1 = 0 THEN GOTO Begin
  35. x1$ = LTRIM$(STR$(x1)): IF x1 > 0 THEN x1$ = "+" + x1$: IF x1 = 1 THEN x1$ = "+"
  36. x0 = c(3) * c(4)
  37. x0$ = LTRIM$(STR$(x0)): IF x0 > 0 THEN x0$ = "+" + x0$
  38. q$ = x2$ + "x" + CHR$(253) + x1$ + "x" + x0$
  39. r$(1) = q$
  40. r$(2) = s$
  41.  
  42. a = x2: b = x1: c = x0
  43. d1# = b
  44. d1# = d1# * b
  45. d2# = a
  46. d2# = 4 * d2# * c
  47. dis! = d1# - d2#
  48.  
  49. r1! = -777: r2! = -777
  50. r$(5) = ""
  51. r$(6) = ""
  52. IF dis! >= 0 THEN
  53.     r$(5) = "bý-4ac" + STR$(dis!)
  54.     dis! = SQR(dis!)
  55.     r$(6) = "dis=" + STR$(dis!)
  56.     r1! = (-b + dis!) / (2 * a)
  57.     r2! = (-b - dis!) / (2 * a)
  58. IF r1! > r2! THEN SWAP r1!, r2!
  59. vx! = (r1! + r2!) / 2
  60. vy! = a * vx! * vx! + b * vx! + c
  61. r$(3) = "x=" + LTRIM$(STR$(r1!)) + "," + LTRIM$(STR$(r2!))
  62. t = ABS(r2! - r1!) + 2 * .5
  63. minx! = r1! - t
  64. maxx! = r2! + t
  65. IF minx! = maxx! THEN
  66.     minx! = minx! - 10
  67.     maxx! = maxx! + 10
  68.  
  69. miny! = -10: maxy! = 10
  70. FOR pass = 0 TO 1
  71.     IF pass = 0 THEN
  72.         s! = (maxx! - minx!) / 100
  73.     ELSE
  74.         Wubba
  75.         s! = (maxx! - minx!) / 300
  76.     END IF
  77.     sx! = (maxx! - minx!) / 180
  78.     sy! = (maxy! - miny!) / 20
  79.     FOR x! = minx! - s! TO maxx! - s! STEP s!
  80.         ty! = Eq!(x!)
  81.         IF pass = 1 THEN CIRCLE (x!, ty!), sx! / 10
  82.     NEXT x!
  83. NEXT pass
  84.  
  85.     i$ = INKEY$
  86.     IF i$ = CHR$(13) THEN GOTO Begin
  87. LOOP UNTIL i$ = CHR$(27)
  88.  
  89. FUNCTION Eq! (x!)
  90.     Eq! = a * x! * x! + b * x! + c
  91.  
  92. SUB Wubba
  93.     z! = 1.8
  94.     minx! = -.5
  95.     IF vx! <= minx! THEN minx! = vx!
  96.     IF r1! <= minx! THEN minx! = r1!
  97.     IF r2! <= minx! THEN minx! = r2!
  98.     minx! = minx! * z!
  99.  
  100.     maxx! = .5
  101.     IF vx! >= maxx! THEN maxx! = vx!
  102.     IF r1! >= maxx! THEN maxx! = r1!
  103.     IF r2! >= maxx! THEN maxx! = r2!
  104.     maxx! = maxx! * z!
  105.  
  106.     IF vy! < 0 THEN
  107.         miny! = vy! + vy! * z!
  108.         maxy! = vy! * -z!
  109.     ELSE
  110.         maxy! = vy! + vy! * z!
  111.         miny! = vy! * -z!
  112.     END IF
  113.     IF miny! = 0 THEN miny! = -5
  114.     IF maxy! = 0 THEN maxy! = 10
  115.  
  116.     WINDOW
  117.     CLS
  118.     WINDOW (minx!, miny!)-(maxx!, maxy!)
  119.  
  120.     LINE (minx!, 0)-(maxx!, 0) ' x axis
  121.     LINE (0, miny!)-(0, maxy!) ' y axis
  122.  
  123.     s = (maxx! - minx!) / 10
  124.     IF s < 1 THEN s = 1
  125.     FOR t! = minx! TO maxx! STEP s / 10 ' x axis ticks
  126.         tx = PMAP(t!, 0)
  127.         ty = PMAP(0, 1)
  128.         WINDOW
  129.         LINE (tx, ty)-(tx, ty + 1), 1
  130.         WINDOW (minx!, miny!)-(maxx!, maxy!)
  131.     NEXT t!
  132.     FOR t = INT(minx!) TO maxx! STEP s ' x numbers
  133.         tx = PMAP(t, 0)
  134.         ty = PMAP(0, 1)
  135.         WINDOW
  136.         LINE (tx, ty)-(tx, ty + 3), 1
  137.         IF t <> 0 THEN _PRINTSTRING (tx - 4, ty + 7), LTRIM$(STR$(t))
  138.         WINDOW (minx!, miny!)-(maxx!, maxy!)
  139.     NEXT t
  140.  
  141.     s = (maxy! - miny!) / 5
  142.     IF s = 0 THEN s = 1
  143.     FOR t! = INT(miny!) TO maxy! STEP s / 10 ' y axis ticks
  144.         tx = PMAP(0, 0)
  145.         ty = PMAP(t!, 1)
  146.         WINDOW
  147.         LINE (tx, ty)-(tx - 1, ty)
  148.         WINDOW (minx!, miny!)-(maxx!, maxy!)
  149.     NEXT t!
  150.     FOR t = INT(miny!) TO maxy! STEP s ' y axis numbers
  151.         tx = PMAP(0, 0)
  152.         ty = PMAP(t, 1)
  153.         WINDOW
  154.         LINE (tx, ty)-(tx - 3, ty)
  155.         IF t <> 0 THEN _PRINTSTRING (tx + 6, ty - 6), LTRIM$(STR$(t))
  156.         WINDOW (minx!, miny!)-(maxx!, maxy!)
  157.     NEXT t
  158.  
  159.     LINE (vx!, vy!)-(vx!, 0), _RGB32(222, 22, 22) ' vertical vertex to x axis
  160.     LINE (vx!, vy!)-(0, vy!), _RGB32(222, 22, 22) ' horizontal to y axis
  161.     r$(4) = "v=" + STR$(vx!) + "," + STR$(vy!)
  162.  
  163.     ml = 0
  164.     FOR i = 1 TO 4
  165.         p = LEN(r$(i))
  166.         IF p > ml THEN ml = p
  167.     NEXT i
  168.     tx = PMAP(0, 0): ty = PMAP(0, 1)
  169.     IF (tx > 400) AND (ty < 300) THEN q = 1
  170.     IF (tx < 400) AND (ty < 300) THEN q = 2
  171.     IF (tx < 400) AND (ty > 300) THEN q = 3
  172.     IF (tx > 400) AND (ty > 300) THEN q = 4
  173.     IF q < 3 THEN ty = 500 ELSE ty = 10
  174.     IF (q = 1) OR (q = 4) THEN tx = 0 ELSE tx = 800 - ml * 10
  175.     WINDOW
  176.     FOR r = 1 TO 4
  177.         _PRINTSTRING (tx + 10, ty + r * 16), r$(r)
  178.     NEXT r
  179.     WINDOW (minx!, miny!)-(maxx!, maxy!)
  180.  
  181.  
It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Generate random quadratic equations and plot them
« Reply #1 on: November 15, 2020, 02:22:07 pm »
Man I wish I had QB64 when I was in HS. Nice work :)

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Generate random quadratic equations and plot them
« Reply #2 on: December 08, 2020, 11:32:40 pm »
Oh damn I missed this!
You're not done when it works, you're done when it's right.