# Qbasicnews.com

## QbasicNews.Com => Challenges => Topic started by: Kevin_theprogrammer on January 27, 2006, 11:35:25 PM

 Title: Challenge: Triangle Function Post by: Kevin_theprogrammer on January 27, 2006, 11:35:25 PM 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... Title: Challenge: Triangle Function Post by: DrV on January 28, 2006, 12:01:12 AM I suppose you mean "draw" when you say "create"?Code:const w as integer = 640, h as integer = 480declare sub drawequitri(length as integer)dim length as integerscreen 12input "Enter the length of the sides: ", lengthclscall drawequitri(length)do until len(inkey\$)loop' assumes 640x480 screen modesub 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 subPrompts user for side length and draws triangle in the center of the screen.  Should work in both QB and FB (only tested with FB). Title: Challenge: Triangle Function Post by: Dio on January 28, 2006, 01:00:04 AM 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!!! :))Code:screen 12,,,1dim mx as integer:dim my as integer:dim mb as integerrandomize timerdo  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 ifif aa = 1 then goto 11loop11for y = 0 to 638for x = 0 to 479  if point(x,y) = 15 then fpx = x:fpy = y:goto 1nextnext1x = 0y = 477yy = ydo  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 = 0loop2x = 638y = 477yy = ydo  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 = 638loop3clsline (fpx,fpy)-(spx,spy),15line (spx,spy)-(tpx,tpy),15line (tpx,tpy)-(fpx,fpy),15sleep Title: Challenge: Triangle Function Post by: Deleter on January 28, 2006, 01:23:39 AM 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 subscreenres 640,480,32EqTri(320,240,100)sleep Title: Challenge: Triangle Function Post by: DefHo on January 28, 2006, 05:44:01 AM Code:DECLARE SUB triangle (x!, y!, size!, angle!, c!)'I assure you the triangles are equalateral. Your screen is just warped.defsng a-zSCREEN 13FOR 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 < .05NEXTSLEEPSUB 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  NEXTEND SUB Title: Challenge: Triangle Function Post by: Kevin_theprogrammer on January 28, 2006, 11:20:19 AM Quoteif you mean draw...Exactly what I meanSo 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... Title: Challenge: Triangle Function Post by: stylin on January 28, 2006, 02:46:06 PM Code:option explicit : option byvaldim 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 ), cend 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 0edit: There ya go. My equilateral function only takes one line. :o :D Title: Challenge: Triangle Function Post by: LooseCaboose on January 29, 2006, 01:25:54 AM 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 13size = 100: half = size / 2DRAW "be=" + VARPTR\$(half) + "d=" + VARPTR\$(size) + "ta60u=" + VARPTR\$(size) + "ta120d=" + VARPTR\$(size) Title: Re: Challenge: Triangle Function Post by: KiZ on January 29, 2006, 12:09:09 PM Quote from: "Kevin_theprogrammer"2 dimensional triangleMind showing me a 3 dimensional triangle? :P Title: Challenge: Triangle Function Post by: marzecTM on January 29, 2006, 01:04:18 PM @kiz to put it in the words of the almighty openglCode: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( ) Title: Challenge: Triangle Function Post by: KiZ on January 29, 2006, 07:25:17 PM Its still a 2d triangle. Just in a 3D environment.A triangle can have no more than 2 dimensions. Thus it is 2D. Foo! Title: Challenge: Triangle Function Post by: marzecTM on January 29, 2006, 09:56:12 PM true it's surface is planar. so is the surface of the world to a certain precision level ( of course it is a curved planar surface ). d'oh? Title: Challenge: Triangle Function Post by: Ralph on January 30, 2006, 01:48:06 AM Quote from: "Deleter"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 subscreenres 640,480,32EqTri(320,240,100)sleepWhy notCode: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+s,y+s*sqr(3)/2)    line -(x,y-s*sqr(3)/2)end subscreenres 640,480,32EqTri(320,240,100)sleepOr, even betterCode:sub EqTri (x as integer, y as integer, s as integer)    t=s*sqr(3)/2    line (x,y-t)-(x-s,y+t)    line -(x+s,y+t)    line -(x,y-t)end subscreenres 640,480,32EqTri(320,240,100)sleep Title: Challenge: Triangle Function Post by: axipher on January 30, 2006, 10:14:46 PM Quote from: "DefHo"Code:DECLARE SUB triangle (x!, y!, size!, angle!, c!)'I assure you the triangles are equalateral. Your screen is just warped.defsng a-zSCREEN 13FOR 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 < .05NEXTSLEEPSUB 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  NEXTEND SUBI still like this one the most, the angle feature is the best.  As well as it supports color.  :D Title: Challenge: Triangle Function Post by: Dr_Davenstein on February 02, 2006, 07:24:45 PM Hey, I made a weirdo!  :lol: Code:Screen 12Type Vector2D    As Single X,YEnd TypeDim As Vector2D P1=>(50,50),P2=>(150,150),P3=>(50,150)For y=0 to 479    For x=0 to 639                If ((P2.X - X) * (P3.Y - Y) - (P3.X - X) * (P2.Y - Y)) / (P2.X - P1.X) * (P3.Y - P1.Y) - (P3.X - P1.X) * (P2.Y - P1.Y)>0 And _           ((P3.X - X) * (P1.Y - Y) - (P1.X - X) * (P3.Y - Y)) / (P2.X - P1.X) * (P3.Y - P1.Y) - (P3.X - P1.X) * (P2.Y - P1.Y)>0 And _           ((P1.X - X) * (P2.Y - Y) - (P2.X - X) * (P1.Y - Y)) / (P2.X - P1.X) * (P3.Y - P1.Y) - (P3.X - P1.X) * (P2.Y - P1.Y)>0 Then           Pset(X,Y),14        End If            NextNextSleep Title: Challenge: Triangle Function Post by: stylin on February 02, 2006, 08:10:17 PM I thought the contest was for a function.  :o Title: Challenge: Triangle Function Post by: Dr_Davenstein on February 02, 2006, 08:26:30 PM blah... It was supposed to make an equilateral triangle too.  :shifty: Title: Challenge: Triangle Function Post by: Deleter on February 03, 2006, 03:02:00 AM Quote from: "Ralph"Quote from: "Deleter"[my code]Why not[better code]Or, even better[even better code]Because I didn't feel like optimizing it, ntm I had never known of that way of doing gfx. I consider myself one step more experienced now  :wink: