
-----------------------------------
RaPsCaLLioN
Tue Mar 29, 2005 11:44 am

Polygon per-pixel control
-----------------------------------
Might not look that impressive but OMG it was hard.  Every time u run it generates a new random polygon.  Each polygon is broken down to the pixel.  And to prove;  I randomly genearted a colour (1, 10)  for display and added a delay so you can see each pixel drawn.
I did this in 137 lines.

It's not 100%.  You'll occasionally see a break in the middle but I'll have that fixed soon.  I'm this much closer now to true texture mapping.

-----------------------------------
jamonathin
Tue Mar 29, 2005 2:33 pm


-----------------------------------
Pretty neat program.  I know it would take away from the cool color effects but drawing lines would be faster, other than that, gj  :)

-----------------------------------
Flikerator
Tue Mar 29, 2005 3:10 pm


-----------------------------------
I would comment on it but I don't have anything to open .rar files :/[/code]

-----------------------------------
[Gandalf]
Tue Mar 29, 2005 5:15 pm


-----------------------------------
Pretty cool, I can see how it took some time :).

To open .rar files get WinRar - almost exactly the same as WinZip, WinAce.

-----------------------------------
jamonathin
Tue Mar 29, 2005 5:15 pm


-----------------------------------
WinRar.  Look it up on google or somthing or go to http://www.download.com/WinRAR/3000-2250_4-10350955.html?tag=lst-0-1

-----------------------------------
Mr. T
Tue Mar 29, 2005 6:03 pm


-----------------------------------
too lazy :roll: ...convert it to an .exe

-----------------------------------
Mazer
Tue Mar 29, 2005 6:27 pm


-----------------------------------
It is an exe. And as it was compiled for Turing, it's a very big exe. rar is a very common compression format for Windows (and oh hey, you can get it for Linux as well) so if you can't be bothered to use it I don't know what you're doing.

-----------------------------------
RaPsCaLLioN
Tue Mar 29, 2005 6:32 pm


-----------------------------------
There ya go.  Added .ZIP

-----------------------------------
GlobeTrotter
Tue Mar 29, 2005 7:52 pm


-----------------------------------
How fast is it w/o the delay?

-----------------------------------
RaPsCaLLioN
Tue Mar 29, 2005 8:14 pm


-----------------------------------
I just updated it.  Click mouse to refresh.  I took out the delay so... well for me it's as fast as I can click.  Also no more errors (white lines).  Try it out again.

-----------------------------------
Naveg
Wed Mar 30, 2005 12:12 am


-----------------------------------
thats pretty cool, wonder what kind of awesome stuff can be done with that

-----------------------------------
GlobeTrotter
Wed Mar 30, 2005 5:22 pm


-----------------------------------
I tried to code it, to see if I could, and I managed to get a very similar effect.  The only real difference between mine and yours is that mine is a lot slower, even without a delay.  Could you maybe post your source so I can compare?


setscreen ("graphics:300,300")
fcn TripleMax (Num : array 0 .. 2 of int) : int
    for i : 0 .. 2
        if Num (i) > Num ((i + 1) mod 3) and Num (i) > Num ((i - 1) mod 3) then
            result Num (i)
        end if
    end for
end TripleMax

fcn TripleMin (Num : array 0 .. 2 of int) : int
    for i : 0 .. 2
        if Num (i) < Num ((i + 1) mod 3) and Num (i) < Num ((i - 1) mod 3) then
            result Num (i)
        end if
    end for
end TripleMin

fcn TriArea (x1 : int, y1 : int, x2 : int, y2 : int, x3 : int, y3 : int) : real
    result abs (x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x3 * y2 - x2 * y1) / 2
end TriArea

proc ShadeTriangle (var Pointx : array 0 .. 2 of int, var Pointy : array 0 .. 2 of int, col : int)
    for x : TripleMin (Pointx) .. TripleMax (Pointx)
        for y : TripleMin (Pointy) .. TripleMax (Pointy)
            if TriArea (Pointx (0), Pointy (0), Pointx (1), Pointy (1), Pointx (2), Pointy (2)) =
                    TriArea (x, y, Pointx (1), Pointy (1), Pointx (2), Pointy (2)) +
                    TriArea (Pointx (0), Pointy (0), x, y, Pointx (2), Pointy (2)) +
                    TriArea (Pointx (0), Pointy (0), Pointx (1), Pointy (1), x, y) then
                drawdot (x, y, col)
            end if

        end for
    end for
end ShadeTriangle

var TempPointx : array 0 .. 2 of int
var TempPointy : array 0 .. 2 of int

for i : 0 .. 2
    TempPointx (i) := Rand.Int (1, maxx - 1)
    TempPointy (i) := Rand.Int (1, maxy - 1)
    Draw.Oval (TempPointx (i), TempPointy (i), 2, 2, 1)
end for

ShadeTriangle (TempPointx, TempPointy, 2)


-----------------------------------
RaPsCaLLioN
Wed Mar 30, 2005 10:37 pm


-----------------------------------
I'd rather not post source just yet.   But... mine does not use any 'if' statements contained in loops.  Nor does it use any multiplication and only 6 divides per polygon.

The only thing is my method is not self-contained.  The two procedures that I used are both relying on an outside array.  I need to fix this before I can progress.  

Could you please explain what the 3 functions you created are calculating?

-----------------------------------
GlobeTrotter
Wed Mar 30, 2005 11:40 pm


-----------------------------------
The first two are pretty useless.  They just give the max or the min of three inputted numbers since turing's min function and max function only work w/ two numbers.

I basically do a for statement, checking every pixel in a box around the triangle, to see if that pixel is inside the triangle.  It is kind of inefficient.  The third function is to calculate the area of a triangle, given three points.  By checking if the sum of the three triangles formed between the given pixel, and the corners, adds up to the area of the triangle, I can know if the pixel is indside the triangle.  If it is, it draws the pixel.

-----------------------------------
zylum
Fri Apr 01, 2005 9:24 pm


-----------------------------------
here's my attempt.. pretty fast, i think its the same way RaPsCaLLioN did his...

setscreen ("offscreenonly")

var X : array 1 .. 3 of int
var Y : array 1 .. 3 of int

proc NewPoints
    for i : 1 .. 3
        X (i) := Rand.Int (maxx div 4, maxx div 4 * 3)
        Y (i) := Rand.Int (maxy div 4, maxy div 4 * 3)
    end for
    var temp : int
    for i : 1 .. 2
        for j : i + 1 .. 3
            if Y (i) < Y (j) then
                temp := X (i)
                X (i) := X (j)
                X (j) := temp
                temp := Y (i)
                Y (i) := Y (j)
                Y (j) := temp
            end if
        end for
    end for
end NewPoints

proc ShadeTriangle
    var b1, m1, b2, m2, b3, m3 : real
    m1 := (Y (1) - Y (3)) / (X (1) - X (3) + 0.0001) + 0.0001
    b1 := Y (1) - m1 * X (1)
    m2 := (Y (1) - Y (2)) / (X (1) - X (2) + 0.0001) + 0.0001
    b2 := Y (2) - m2 * X (2)
    m3 := (Y (2) - Y (3)) / (X (2) - X (3) + 0.0001) + 0.0001
    b3 := Y (3) - m3 * X (3)

    for decreasing y : Y (1) .. Y (2)
        for x : min (round ((y - b1) / m1), round ((y - b2) / m2)) .. max (round ((y - b1) / m1), round ((y - b2) / m2))
            drawdot (x, y, Rand.Int (1, 10))
        end for
    end for
    for decreasing y : Y (2) .. Y (3)
        for x : min (round ((y - b1) / m1), round ((y - b3) / m3)) .. max (round ((y - b1) / m1), round ((y - b3) / m3))
            drawdot (x, y, Rand.Int (1, 10))
        end for
    end for
    View.Update
end ShadeTriangle

var mx, my, md : int
loop
    NewPoints
    ShadeTriangle
    loop
        mousewhere (mx, my, md)
        exit when md = 1
    end loop
    cls
end loop


-57 lines and sometimes it draws a couple of polygons in a single mouse click (yes that fast). although some of the larger ones take a bit more time.

-----------------------------------
RaPsCaLLioN
Fri Apr 01, 2005 9:36 pm


-----------------------------------
That is pretty fast.  Mine's a tad quicker because your running 11 divides per polygon.  And if you do this to your code...

import GUI in "%oot/lib/GUI"
setscreen ("offscreenonly")

var X : array 1 .. 3 of int
var Y : array 1 .. 3 of int

proc NewPoints
    for i : 1 .. 3
        X (i) := Rand.Int (maxx div 4, maxx div 4 * 3)
        Y (i) := Rand.Int (maxy div 4, maxy div 4 * 3)
    end for
    var temp : int
    for i : 1 .. 2
        for j : i + 1 .. 3
            if Y (i) < Y (j) then
                temp := X (i)
                X (i) := X (j)
                X (j) := temp
                temp := Y (i)
                Y (i) := Y (j)
                Y (j) := temp
            end if
        end for
    end for
end NewPoints

proc ShadeTriangle
    var b1, m1, b2, m2, b3, m3 : real
    m1 := (Y (1) - Y (3)) / (X (1) - X (3) + 0.0001) + 0.0001
    b1 := Y (1) - m1 * X (1)
    m2 := (Y (1) - Y (2)) / (X (1) - X (2) + 0.0001) + 0.0001
    b2 := Y (2) - m2 * X (2)
    m3 := (Y (2) - Y (3)) / (X (2) - X (3) + 0.0001) + 0.0001
    b3 := Y (3) - m3 * X (3)

    for decreasing y : Y (1) .. Y (2)
        for x : min (round ((y - b1) / m1), round ((y - b2) / m2)) .. max (round ((y - b1) / m1), round ((y - b2) / m2))
            drawdot (x, y, 7)
        end for
    end for
    for decreasing y : Y (2) .. Y (3)
        for x : min (round ((y - b1) / m1), round ((y - b3) / m3)) .. max (round ((y - b1) / m1), round ((y - b3) / m3))
            drawdot (x, y, 7)
        end for
    end for
    View.Update
end ShadeTriangle

var mx, my, md : int
GUI.SetBackgroundColour (7)
loop
    GUI.SetBackgroundColour (7)
    NewPoints
    Draw.FillPolygon (X, Y, 3, 60)
    ShadeTriangle
    loop
        mousewhere (mx, my, md)
        exit when md = 1
    end loop
    cls
end loop


We can see some flaws.

-----------------------------------
Drakain Zeil
Wed Apr 06, 2005 6:14 am


-----------------------------------
Polygon resources.

http://claymore.engineer.gvsu.edu/~jackh/eod/software/software-108.html
http://www.math.montana.edu/Rweb/Rhelp/polygon.html
