Author Topic: Acceleration  (Read 1536 times)

Galileo

  • Guest
Acceleration
« on: January 25, 2018, 05:04:32 PM »
Yabasic is not the fastest interpreter in the world, but under certain conditions, that's no problem.

Code: [Select]
// Yabasic 2.78 by Galileo 12/2017

torad = 0.0174533

clear screen

w = 850 : h = 700
cx = w / 2 : cy = h / 2

open window 850, 700
backcolor 0, 0, 0

FOR j = 1 TO 50
    FOR n = 0 TO 360
    if not mod(n, j) then
        clear window
        color 255, 255, 255 : line cx + 150, cy, cx + 450, cy // mesa
        color 255, 0, 0 : circle cx, cy, 5 // centro de la rueda
        color 0, 255, 0 : circle cx, cy, 100 // rueda
        color 0, 0, 255 : c = n * torad : x = COS(c) * 100 : y = SIN(c) * 100 : dx = cx + x : dy = cy + y : circle dx, dy, 5 // anclaje a la rueda
        line cx, cy, dx, dy // radio de la rueda
circle dx + 300, 325, 5 // anclaje al objeto
line dx, dy, dx + 300, 325 // barra de anclaje
color 128, 128, 128 : rectangle dx + 300 - 30, 325 - 20, dx + 300 + 30, 325 + 20 // objeto
pause .02
end if
    NEXT n
NEXT j
« Last Edit: January 25, 2018, 05:06:23 PM by Galileo »

Peter

  • Guest
Re: Acceleration
« Reply #1 on: January 26, 2018, 08:47:20 PM »
Made some small modifications in the positioning, and also in the timing parameters for the GIF to work nicely. Most browsers do not support a GIF frame delay lower than 0.02 seconds.

Code: [Select]
' Yabasic 2.78 by Galileo 12/2017
' Ported to BaCon by PvE

INCLUDE canvas-gd
OPTION VARTYPE FLOATING

torad = 0.0174533

w = 640 : h = 400
cx = w / 2 - 100 : cy = h / 2

WINDOW("accel", w, h)
DELAY(2) : ' Delay between GIF frames in 1/100 seconds, 2 = 0.02 seconds

FOR j = 1 TO 5
    FOR n = 0 TO 359 STEP 10
        IF NOT(MOD(n, j)) THEN
            INK(0, 0, 0, 255)
            CLS
            INK(255, 255, 255, 255) : LINE(cx, cy, cx + 350, cy) : ' mesa
            INK(255, 0, 0, 255) : CIRCLE(cx, cy, 5, 5, 0) : ' centro de la rueda
            INK(0, 255, 0, 255) : CIRCLE(cx, cy, 100, 100, 0) : ' rueda
            INK(0, 0, 255, 255) : c = n * torad : x = COS(c) * 100 : y = SIN(c) * 100 : dx = cx + x : dy = cy + y : CIRCLE(dx, dy, 5, 5, 0) : ' anclaje a la rueda
            LINE(cx, cy, dx, dy) : ' radio de la rueda
            CIRCLE(dx + 220, cy-25, 5, 5, 0) : ' anclaje al objeto
            LINE(dx, dy, dx + 220, cy-25) : ' barra de anclaje
            INK(128, 128, 128, 255) : SQUARE(dx + cx, cy-25, 30, 20, 0) : ' objeto
            SYNC
        ENDIF
    NEXT n
NEXT j

« Last Edit: January 26, 2018, 09:01:23 PM by Peter »

B+

  • Guest
Re: Acceleration
« Reply #2 on: January 27, 2018, 05:39:33 AM »
Code: [Select]
'Piston_Acceleration.bas for SmallBASIC v 0.12.11 (B+=MGA) 2018-01-26
' translated and extremely modified from
'// Yabasic 2.78 by Galileo 12/2017


'====================================================================
'
'    Instructions - use g for gas and b for brake'
'
'====================================================================

w = xmax: h = ymax
cx = w / 2 : cy = h / 2
dir = -1 : d = 20
while 1
  for n = 0 TO 359
      cls
      if n mod 60 = 0 then k = inkey  'THIS SOLVES PROBLEM OF INKEY SLOW DOWN IN GRAPHICS LOOPS
      if k = "g" then
        if d - 1 > 0 then d= d - 1
      elif k = "b" then
        if d+1 < 40 then d = d + 1
      end if
      k = ""
      locate 1, 1 : ? d   
      x = -COS(rad(n)) * 100 : y = SIN(rad(n)) * 100
      dx = cx + x : dy = cy + y
      color 9
      circle cx, cy, 110, 1, 9 filled
      circle cx, cy, 90, 1, 0 filled
      star cx, cy, 35, 140, 20, -n
      line cx + 150, cy, cx + 450, cy, 15
      rect dx + 300 - 30, cy - 40, dx + 300 + 30, cy - 1, 12 filled
      color 10
      thickline dx, dy, dx + 300, cy - 20, 10
      circle dx + 300, cy - 20, 5, 1, 14 filled
      circle dx, dy, 5, 1, 14 filled
      circle dx + 300, cy - 20, 3, 1, 0
      circle dx, dy, 3, 1, 0
      showpage
      delay d
  next
wend

sub thickline(x1, y1, x2, y2, thick) 'this draws a little rectangle
  local arr, r, dx, dy, perpA1, perpA2
  dim arr()
  r = thick/2
  dx = x2 - x1
  dy = y2 - y1
  perpA1 = atan2(dy, dx) + pi/2
  perpA2 = perpA1 - pi
  arr << x1 + r * cos(perpA1) 'corner 1
  arr << y1 + r * sin(perpA1)
  arr << x2 + r * cos(perpA1) 'corner 2
  arr << y2 + r * sin(perpA1)
  arr << x2 + r * cos(perpA2) 'corner 3
  arr << y2 + r * sin(perpA2)
  arr << x1 + r * cos(perpA2) 'corner 4
  arr << y1 + r * sin(perpA2)
  arr << x1 + r * cos(perpA1) 'back to first corner
  arr << y1 + r * sin(perpA1)
  drawpoly arr filled
end

sub star( x, y, rInner, rOuter, nPoints, angleOffset)
  ' x, y are same as for circle,
  ' rInner is center circle radius
  ' rOuter is the outer most point of star
  ' nPoints is the number of points,
  ' angleOffset = angle offset IN DEGREES, it will be converted to radians in sub
  ' this is to allow us to spin the polygon of n sides
  pAngle = RAD(360 / nPoints)  :  radAngleOffset = RAD(angleOffset)
  x1 = x + rInner * cos(radAngleOffset)
  y1 = y + rInner * sin(radAngleOffset)
  for i = 0 to nPoints - 1
    x2 = x + rOuter * cos(i * pAngle + radAngleOffset + .5 * pAngle)
    y2 = y + rOuter * sin(i * pAngle + radAngleOffset + .5 * pAngle)
    x3 = x + rInner * cos((i + 1) * pAngle + radAngleOffset)
    y3 = y + rInner * sin((i + 1) * pAngle + radAngleOffset)
    line x1, y1, x2, y2, 9
    line x2, y2, x3, y3, 9
    x1 = x3 : y1 = y3
  next
  paint x, y, 9
end sub