Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge: Triangle Function
#1
Try to make a function that willl create an equilateral 2 dimensional triangle of any size that a user wishes using pure QB/FB code. Try to make the function as small as possible code-wise, not line-wise to reduce topic scrolling...
[Image: 1403.png]
^ Infrosoft
http://www.thecodeyouneed.us.to/ - A wiki of source code, mostly in PHP and FreeBASIC
http://www.osadvocacy.uk.to/ - Your opinion matters no matter your OS
Reply
#2
I suppose you mean "draw" when you say "create"?

Code:
const w as integer = 640, h as integer = 480
declare sub drawequitri(length as integer)
dim length as integer
screen 12
input "Enter the length of the sides: ", length
cls
call drawequitri(length)
do until len(inkey$)
loop

' assumes 640x480 screen mode
sub drawequitri(length as integer)
    dim height as integer
    height = sqr(3) * abs(length) / 2
    line ((w - length) \ 2, (h - height) \ 2)-step(length, 0)
    line ((w - length) \ 2, (h - height) \ 2)-step(length \ 2, height)
    line ((w + length) \ 2, (h - height) \ 2)-step(-length \ 2, height)
end sub

Prompts user for side length and draws triangle in the center of the screen. Should work in both QB and FB (only tested with FB).
Reply
#3
if you mean draw then i made one where you actully draw it:
(i know i used alot of goto's. i don't care!!! Smile)
Code:
screen 12,,,1
dim mx as integer:dim my as integer:dim mb as integer
randomize timer
do
  getmouse mx,my,,mb
  if mb = 1 then
    aa = 1
    do
      ox = mx:oy = my
      getmouse mx,my,,mb
      line (mx,my)-(ox,oy),15
    loop until mb <> 1
  end if
if aa = 1 then goto 11
loop
11
for y = 0 to 638
for x = 0 to 479
  if point(x,y) = 15 then fpx = x:fpy = y:goto 1
next
next
1
x = 0
y = 477
yy = y
do
  if point(x,y) = 15 then spx = x:spy = y:goto 2
  x = x +1
  y = y +1:if y = 479 then yy = yy - 1:y = yy:x = 0
loop
2
x = 638
y = 477
yy = y
do
  if point(x,y) = 15 then tpx = x:tpy = y:goto 3
  x = x -1
  y = y +1:if y = 479 then yy = yy - 1:y = yy:x = 638
loop
3
cls
line (fpx,fpy)-(spx,spy),15
line (spx,spy)-(tpx,tpy),15
line (tpx,tpy)-(fpx,fpy),15
sleep
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Reply
#4
Code:
sub EqTri (x as integer, y as integer, s as integer)
    line (x,y-s*sqr(3)/2)-(x-s,y+s*sqr(3)/2)
    line (x,y-s*sqr(3)/2)-(x+s,y+s*sqr(3)/2)
    line (x-s,y+s*sqr(3)/2)-(x+s,y+s*sqr(3)/2)
end sub
screenres 640,480,32
EqTri(320,240,100)
sleep
[Image: freebasic.png]
Reply
#5
Code:
DECLARE SUB triangle (x!, y!, size!, angle!, c!)
'I assure you the triangles are equalateral. Your screen is just warped.

defsng a-z
SCREEN 13

FOR i = 10 TO 255
  w = 0
  FOR j = i - 10 TO i
    OUT 968, j
    OUT 969, w
    OUT 969, w
    OUT 969, w
    w = w + 6.3
  NEXT
  x = SIN(i / 35) * 50 + 160
  y = SIN(i / 25) * 50 + 100
  triangle x, y, SIN(i / 100) * 50 + 10, i * 10, i
  t = TIMER
  DO: LOOP WHILE TIMER - t < .05
NEXT

SLEEP

SUB triangle (xoff, yoff, size, angle, c)
  DIM x(2), y(2)
  FOR i = 0 TO 2
     x(i) = COS((i * 120 + angle) * 3.14159 / 180) * size + xoff
     y(i) = SIN((i * 120 + angle) * 3.14159 / 180) * size + yoff
  NEXT
  FOR i = 0 TO 2
    LINE (x(i), y(i))-(x((i + 1) MOD 3), y((i + 1) MOD 3)), c
  NEXT
END SUB
hat were we arguing about again?
Reply
#6
Quote:if you mean draw...
Exactly what I mean

So far I'd say deleter made the smallest function that adheres to the challenge and flexible enough. So so far I'd say he's got it...
[Image: 1403.png]
^ Infrosoft
http://www.thecodeyouneed.us.to/ - A wiki of source code, mostly in PHP and FreeBASIC
http://www.osadvocacy.uk.to/ - Your opinion matters no matter your OS
Reply
#7
Code:
option explicit : option byval

dim shared as double pi => 4.0 * atn( 1 )

'' DrawIT( x, y, base, height, angle (radians), color )
sub DrawIT( x as integer, y as integer,  b as single, h as single, a as single, c as uinteger )
    dim as integer deltaX => b/2*sin(a), deltaY => b/2*cos(a) : a += pi/2
    
    line( x + deltaX, y + deltaY ) - ( x - deltaX, y - deltaY ), c
    line step( 0,0 ) - ( x + h*sin(a), y + h*cos(a) ), c
    line step( 0,0 ) - ( x + deltaX, y + deltaY ), c
end sub

'' DrawET( x, y, base, angle (radians), color )
sub DrawET( x as integer, y as integer,  b as single,  a as single, c as uinteger )
    DrawIT( x,y, b,b*sin(pi/3), a, c )
end sub

    screenres 640,480,32
    
    dim as double r
    for r=0 to 2*pi step pi/45
        DrawET( 320,240, 100*abs(atn(r)),r, r * &h0000ff/(2*pi) )
    next
    
    sleep : end 0

edit: There ya go. My equilateral function only takes one line. :o Big Grin
stylin:
Reply
#8
Tested with QB, don't know if FB supports the draw command properly. Just set the variable size to the length you want the sides to be.
Code:
SCREEN 13
size = 100: half = size / 2
DRAW "be=" + VARPTR$(half) + "d=" + VARPTR$(size) + "ta60u=" + VARPTR$(size) + "ta120d=" + VARPTR$(size)
esus saves.... Passes to Moses, shoots, he scores!
Reply
#9
Quote:2 dimensional triangle

Mind showing me a 3 dimensional triangle? :P
Reply
#10
@kiz to put it in the words of the almighty opengl

Code:
glBegin( GL_TRIANGLES )
glVertex3f( -0.5, -0.5, -0.5 )
glVertex3f( 0.0, 0.5, 0.0 )
glVertex3f( 0.5, -0.5, 0.5 )
glEnd( )
quote="NecrosIhsan"]
[Image: yagl1.png]
[/quote]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)