Pic v3 - December 14 2011 - flood fill added

Will upload this evening (no outgoing email from my ipad at work for reasons to nutty to go into).

Fantastic! I should add these to my “balls on sticks” code to make the vertices more realistic.

Add to the Pic class (or just modify and include the code):

``````-- http://rosettacode.org/wiki/Draw_a_sphere
local function normalize (vec)
len = math.sqrt(vec[1]^2 + vec[2]^2 + vec[3]^2)
return {vec[1]/len, vec[2]/len, vec[3]/len}
end

local light = normalize{30, -30, -50}

local function dot (vec1, vec2)
d = vec1[1]*vec2[1] + vec1[2]*vec2[2] + vec1[3]*vec2[3]
return d < 0 and -d or 0
end

local function m255(a, b)
local r = a * b
if (r>255) then r=255 end
return r
end

local i,x,j,vec,b
x = i + .5
y = j / 2 + .5
if x^2 + y^2 <= radius^2 then
vec = normalize{x, y, math.sqrt(radius^2 - x^2 - y^2)}
b = dot(light,vec) ^ k + ambient
--intensity = math.floor ((1 - b) * 255)
local ba = b+ambient
self.image:set(x+self.width/2, y+self.height/2,
m255(self.r, ba), m255(self.g, ba),
m255(self.b, ba), self.a)
end
end
end
end
``````

Syntax is:

``````s = Pic(99, 99)
s:setcolor(255, 100, 0, 255)
s:sphere(45, 2, 0.1) -- radius, k, ambient light
sprite(s.image, WIDTH/2, HEIGHT/2)
``````

I’m kind of scared that you might write a ray-tracer next, @Bortels.

@Bortels: maybe a stupid question: is it possible to create facets with your routines? Let’s assume I do have a mesh grid, and I want to fill this grid with a certain color, or colormap. How can I use the image or pic class for doing that? I am currently working on a version using the scanline algorithm with normal line commands. This works, but is a bit slow (and still buggy…). I was thinking that using the image or pic class could do the job faster. How difficult would it be, if you want to update the shape of the bounding box e.g. Due to rotation?

You wouldn’t get good 3d shapes from this; it’s pretty much a sphere. And the filled poly stuff won’t help, because there’s no support for shading.

ray tracer, huh?

This is what I currently have.

http://crazyed.posterous.com/betabeta3dmeshv10

It’s beta square, since it does not work perfectly yet. For closed surfaces there are rendering issues (swap between inner and outer side …), and for open structures there is a specific angle alpha at about -7°, where you can see rendering effects. If you go further to -180° situations improves again. I have included a set of functions, which you might want to try.

Currently I have no idea where to seek the problems. The detection, which facet is showing top or bottom works so lala, say almost 90%. If somebody of the more experienced math guys could help me out here, it would be perfect!!! I guess this is still the crucial part in the whole code.

Maybe in one of the next Codea releases there will be support of such 3D plotting as well? Would be simply great!

@Bortels: this is the background of my question about using the fill polygon code of you. It’s anyway fascinating, how such rendering works, and much work is behind all those beautiful math function drawings with colored facets … Wish, I could crack the nut.

Can somebody tell me how to place a photo here? I made a screenshot, but I am not able to copy that here … Any trick?

So - as I research ray tracing, I realize that that’s what this sphere is, grossly oversimplified - it figures out the color at any point by comparing the vector of the viewer with the vector of the normal to the sphere at that point. NO REALLY.

Just sharing.

@CrazyEd… Simeon posted the syntax for placing a photo in the forum earlier today in another thread…

``````![Image Name](url)

For example

![My Image](http://twolivesleft.com/myImage.png)
``````

@Bortels: would have been a miracle if it would work that simple … Any idea how to improve? Or is the basic concept already crap?

@Blanchot: many thanks for this hint. But I fear I have not got the point. I have the screenshot in my photo archive on my iPad, so still the question how to post that.

@CrazyEd you’ll have to upload it somewhere, like posterous or twitpic (or your own server). Then you can get the image address and use that to embed it in a post here.

If you’re running a recent iOS, just tweet it - it’s built-in, and fast and easy.

@Bortels: do you intend of writing a Ray tracer code? Basically with my 3D mesh project I had had several issues, which I was not able to solve. First I am sure that the current 3D to 2D method is just an approximation. I have the feeling that the perspective is somewhat wrong. On the other side I wanted to use just azimuth and elevation as rotation angles, which means to kick away one go the three angles as defined in your cube code. Do you have any idea how a transform could look alike? Secondly I was thinking that calculating the normal of a plane as defined by three points would already give a good hint, if the facet is visible or not. As it seems for non-closed objects this gives some reasonable result, but I start to doubt, based on your statement about grossly simplification, that this is the correct way of doing that. For a ray tracer, I guess you also need the normal vector of a facet, or not? If you would like to introduce colored facets on your cube, how would you determine the visibility of a facet, and if the object is even more complex, detect, if a facet is hidden (either partly or completely) by another one?

@Simeon, @Bortels: thanks for the hints!

Me writing (or really, porting) a ray tracer would just be a stunt - I have no real interest in taking something like that to where it needs to be, or more correctly - no real time. For every project I publish, I have 2 that I got halfway done with and are sitting waiting for me to finish (really!). I did read up on it yesterday, found some good links - I’ll try to post them later. My understanding is that you’re basically correct - you trace the ray back to the object it hits, take a normal to that surface, and calculate the light intensity based on the angle between the ray and the normal of the surface.

Uploaded version 3 with flood fill

http://bortels.posterous.com/pic-v3

Just to be clear my ray tracer comment was a joke

@Simeon: but it would be great to have such tools available. And Codea is such great platform which for sure would allow that. Might be that my 3D mesh routine is not 100% perfect, and it contains bugs, but I was impressed how fast in the end one can realize that. I simply would love to use Codea more and more for such kind of problems, I only need to learn more about the capabilities and possibilities of Codea. But it seems to me that you can do almost anything with Codea, if you just know how to do it. I guess I did not mention that yet, but thanks a lot for this app, Simeon!!!

Joke or not, I am still thinking of doing it, just to be awesome