=help!= random circles in triangle
Author 
Message 
newbie

Posted: Sun Feb 16, 2003 4:49 am Post subject: =help!= random circles in triangle 


anyone know how to keep the random circles within a triangle?
i'm able to make the restriction within the box of the upright triangle but i can't make it so that it's all inside the triangle
plz help 





Sponsor Sponsor



Tony

Posted: Sun Feb 16, 2003 6:09 am Post subject: (No subject) 


well you gotta figure out the function of the sloped line and restrict that
thats our triangle... You can easily place restrictions such as above bottom line and to the right of vertical line. Now all thats left is below and to the left of sloped line and thats a problem...
But if you find the function of the line, its not so much of a problem because you'll know where the line is in comparisment to the circle
So the function is... Y =  (rise / run)X + rise where rise is vertical heigh and run is horizontal.
This way after randomly selecting circle location, you use circle's X value to calculate line's Y value (X of the line will be the same). After that you can see if the line is above or below the circle. If its above, congrads, you can draw your circle now.
code: 
var x,y:int
var lineY:int
var c:int
loop
randint(x,0,maxx)
randint(y,0,maxy)
randint(c,1,255)
lineY := round((maxy/maxx)*x + maxy)
if y < lineY then
drawfilloval (x,y,5,5,c)
end if
end loop


Tony's programming blog. DWITE  a programming contest. 




newbie

Posted: Sun Feb 16, 2003 6:19 am Post subject: (No subject) 


thnx so much for replying
i'm actually working on a christmas tree with randomly placed bulbs on it..
so do i use the same idea that you posted above? 





Izzy

Posted: Sun Feb 16, 2003 11:47 am Post subject: (No subject) 


Everyone loves a christmas tree
code: 
% Sets up screen
setscreen ("graphics:svga")
setscreen ("graphics:800;600")
% Sets up the tree
drawline (50, 50, 750, 50, black)
drawline (50, 50, 400, 550, black)
drawline (750, 50, 400, 550, black)
drawbox (380, 20, 420, 50, black)
% Adds color to spice up our life
Draw.Fill (100, 100, green, black)
Draw.Fill (400, 30, brown, black)
Draw.Fill (5, 100, black, black)
% Variable declaration
var BulbX, BulbY,OnceAgainColorIsCooler : int
% Repeats this code 250 times because a tree with one bulb is a tree no body wants
for SetupBulbs: 1..250
% Randomly selects number for the X and Y value and The color
% I minimized the Random numbers for X and Y because there is no point in randomly generating a number that wont be used
% ie the bottom corners of the tree are the min and max value of x
% and the bottom of the tree and the very top point of the tree is the min and max y value
randint (BulbX, 80, 720)
randint (BulbY, 65, 525)
% svga sure has alot of perty colors
randint (OnceAgainColorIsCooler,1,255)
% Checks to see if the bulb is on the tree
if BulbY +25 < (1.43 * BulbX  21.5) and BulbY +25 < (1.43 * BulbX + 1122.5) then
% Draws the bulb
drawfilloval (BulbX, BulbY, 10, 10, OnceAgainColorIsCooler)
end if
end for
% Final touches on the tree
drawfillstar(370,530,430,580,yellow)

There are two portions of the if statement..
1. BulbY +25 < (1.43 * BulbX  21.5)
2. BulbY +25 < (1.43 * BulbX + 1122.5)
The first portion checks to see if the bulb is on the left side of the tree, the second portion checks to see if the bulb is on the right side of the tree.
The standard equation for a line is y=mx + b, where m is the slope and b is the yintercept.
I've calculated the info I need on my own, if you don't know how I got it let me know. But what we need is the slope of both slanted lines and the yintercept. The slope is 1.43 for both (1.43 for the second because it is flipped). The yintercept is 21.5 for the first and +1122.5 for the second.
Now i'll explain what's happening, the point is to calculate the Y value for the cooresponding X value for the branch (or edge of the tree) and compare it to the Y Value of bulb and decide whether or not to draw a bulb (circle). First of all we know that the X value of the bulb is the same as the X value of the tree branch. So we can sub x into the formula y=1.43x  21.5 or y=1.43x +1122.5 and solve for y. When we have y we check if the Y value for the bulb is less than the y value of the branch, if it is we know it's on the tree. I added 25 to the yvalue of the bulb so we don't have any bulbs half on, half off the tree. 






