Qbasicnews.com
November 21, 2019, 02:58:46 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: Challenge: Triangle Function  (Read 10304 times)
Kevin_theprogrammer
Ancient Guru
****
Posts: 646


Mister Chaucer


WWW
« 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...
Logged


^ 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
DrV
Na_th_an
*****
Posts: 1553



WWW
« Reply #1 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).
Logged
Dio
I hold this place together
*****
Posts: 874



« Reply #2 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!!! Smiley)
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
Logged

quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Deleter
Na_th_an
*****
Posts: 1292



WWW
« Reply #3 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
Logged

DefHo
Member
*
Posts: 74



« Reply #4 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
Logged

hat were we arguing about again?
Kevin_theprogrammer
Ancient Guru
****
Posts: 646


Mister Chaucer


WWW
« Reply #5 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...
Logged


^ 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
stylin
Ancient QBer
****
Posts: 445


« Reply #6 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. Shocked Cheesy
Logged

stylin:
LooseCaboose
I hold this place together
*****
Posts: 981



« Reply #7 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)
Logged

esus saves.... Passes to Moses, shoots, he scores!
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #8 on: January 29, 2006, 12:09:09 PM »

Quote from: "Kevin_theprogrammer"
2 dimensional triangle


Mind showing me a 3 dimensional triangle? :P
Logged
marzecTM
Ancient Guru
****
Posts: 688


« Reply #9 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( )
Logged

quote="NecrosIhsan"]

[/quote]
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #10 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!
Logged
marzecTM
Ancient Guru
****
Posts: 688


« Reply #11 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?
Logged

quote="NecrosIhsan"]

[/quote]
Ralph
Ancient Guru
****
Posts: 544


« Reply #12 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
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
axipher
Ancient Guru
****
Posts: 544



« Reply #13 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.  Cheesy
Logged
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #14 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
Logged
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!