Title: Challenge: Triangle FunctionPost 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 FunctionPost 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 = 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). Title: Challenge: Triangle FunctionPost 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,,,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 Title: Challenge: Triangle FunctionPost 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 sub screenres 640,480,32 EqTri(320,240,100) sleep Title: Challenge: Triangle FunctionPost 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-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 Title: Challenge: Triangle FunctionPost by: Kevin_theprogrammer on January 28, 2006, 11:20:19 AM
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... Title: Challenge: Triangle FunctionPost by: stylin on January 28, 2006, 02:46:06 PM
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 :D Title: Challenge: Triangle FunctionPost 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 13 size = 100: half = size / 2 DRAW "be=" + VARPTR$(half) + "d=" + VARPTR$(size) + "ta60u=" + VARPTR$(size) + "ta120d=" + VARPTR$(size) Title: Re: Challenge: Triangle FunctionPost by: KiZ on January 29, 2006, 12:09:09 PM
Quote from: "Kevin_theprogrammer" 2 dimensional triangle Mind showing me a 3 dimensional triangle? :P Title: Challenge: Triangle FunctionPost by: marzecTM on January 29, 2006, 01:04:18 PM
@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( ) Title: Challenge: Triangle FunctionPost 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 FunctionPost 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 FunctionPost 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 sub screenres 640,480,32 EqTri(320,240,100) sleep Why not 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+s,y+s*sqr(3)/2) line -(x,y-s*sqr(3)/2) end sub screenres 640,480,32 EqTri(320,240,100) sleep Or, even better Code: 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 sub screenres 640,480,32 EqTri(320,240,100) sleep Title: Challenge: Triangle FunctionPost 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-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 I still like this one the most, the angle feature is the best. As well as it supports color. :D Title: Challenge: Triangle FunctionPost by: Dr_Davenstein on February 02, 2006, 07:24:45 PM
Hey, I made a weirdo! :lol:
Code: Screen 12 Type Vector2D As Single X,Y End Type Dim 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 Next Next Sleep Title: Challenge: Triangle FunctionPost by: stylin on February 02, 2006, 08:10:17 PM
I thought the contest was for a function. :o
Title: Challenge: Triangle FunctionPost 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 FunctionPost 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: |