Challenge: Faking 3D extrusion of a polygon

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
micmac
Posts: 49
Joined: Mon May 30, 2011 9:00 am

Challenge: Faking 3D extrusion of a polygon

Post by micmac » Mon May 30, 2011 8:02 pm

Fake3D 2.rev.zip
Second attempt
(1.64 KiB) Downloaded 270 times
Fake3D.rev.zip
First attempt
(1.83 KiB) Downloaded 277 times
Fake3D.rev.zip
First attempt
(1.83 KiB) Downloaded 277 times
Hi
This is a challenge of making a fake 3D extrusion of a random shaped polygon.

In very simple cases you can just copy the polygon, put it behind with a little offset and shade it a little darker than the front polygon.

But if the offset gets a little larger then one can see that the two polygons doesn't form one object.

This is a far as I have come:
First attempt Looks nice but uses two grc for the extrusion

Second attempt only uses one but is not working in all situations
Can you make it better?


Mic

micmac
Posts: 49
Joined: Mon May 30, 2011 9:00 am

Re: Challenge: Faking 3D extrusion of a polygon

Post by micmac » Tue May 31, 2011 1:26 pm

Ok so this is a little better:

on mouseUp
lock screen
put points of grc P1 into points1
put right of grc P1 into Px -- at the right of P1 the flow of the polygon goes in the opposite direction
repeat with p = 1 to number of lines of points1
if item 1 of line p of points1 = Px then put item 2 of line p of points1 into Py
end repeat

set points of grc P2 to points1
move grc P2 relative 35,0 in 1 millisec
put points of grc P2 into points2

put line 1 of points1 & return after newPoints
put line 1 of points2 & return after newPoints


repeat with i = 2 to number of lines of points1-1
if item 2 of line i of points1 ≤ Py then
if not within(grc P2,line i of points1) then put line i of points1 & return after newPoints
if not within(grc P1,line i of points2) then put line i of points2 & return after newPoints
end if

if item 2 of line i of points1 > Py then
if not within(grc P1,line i of points2) then put line i of points2 & return after newPoints
if not within(grc P2,line i of points1) then put line i of points1 & return after newPoints
end if

if within(grc P1,line i of points2) and within(grc P2,line i of points1) then put line i of points2 & return after newPoints
end repeat

set points of grc p2 to newpoints
end mouseUp

put this script into btn "Fake 3D" of second attempt stack

the change is this portion:
put right of grc P1 into Px -- at the right of P1 the flow of the polygon goes in the opposite direction
repeat with p = 1 to number of lines of points1
if item 1 of line p of points1 = Px then put item 2 of line p of points1 into Py
end repeat

Mic

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4002
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Challenge: Faking 3D extrusion of a polygon

Post by bn » Wed Jun 01, 2011 8:39 pm

Hi Mike,

do you accept cheats in the Challenge? :) Afterall I only cheat 35 times as often as you do, but in smaller steps. So that levels that.

Code: Select all

on mouseUp
   lock screen
   put points of grc P1 into points1
   
   put 35 into tHorzOff
   if tHorzOff < 0 then
      put -1 into tInc
   else
      put 1 into tInc
   end if
   
   put abs (tHorzOff) into tHorzOff
   
   set the points of graphic "p2" to points1
   set points of grc P2 to points1
   
   put "" into tArray
   
   repeat with i = 1 to tHorzOff
      move grc "p2" relative tInc,0
      put the points of grc "p2" into tArray[i]
   end repeat
   
   put points1 into newPoints
   
   put the the keys of tArray into tList
   repeat for each line aLine in tList
      put cr& cr & tArray[aLine] after newPoints
   end repeat
   
   set points of grc p2 to newpoints
end mouseUp
It is made of 2 graphics, has no noticeable artifacts, those where the conditions, right?

As a bonus: Can be set to horizontal and vertikal, positive and negative offsets.



By the way there is at least one gotcha in your code. The within function returns false for the points on the right border and the bottom border although one would think it is within.

Code: Select all

on mouseUp
   put the points of grc "p1" into tData
   repeat for each line aPoint in tData
      if not within(grc "p1", aPoint) then
         put aPoint & cr after tCollect
      end if
   end repeat
   delete last char of tCollect -- return
   put tCollect
end mouseUp
But otherwise I failed miserably to get this working in a 'honest' way.

Someone into Polygons and Mathematics should have a look at this.

If you find a 'true' algorithm I would be very interested.

Kind regards

Bernd
Last edited by bn on Thu Jun 02, 2011 12:04 am, edited 1 time in total.

micmac
Posts: 49
Joined: Mon May 30, 2011 9:00 am

Re: Challenge: Faking 3D extrusion of a polygon

Post by micmac » Wed Jun 01, 2011 10:17 pm

OK you did it Bernd!

As I understand it it is essential 35 copies of the polygon in one object.

But it looks right.

So Rev has to work a little. Even if I have a very fast machine there is a notible delay.

Thanks for the answer Bernd, we have actually have had private mail contact for some time ago.

Mic

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4002
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Challenge: Faking 3D extrusion of a polygon

Post by bn » Wed Jun 01, 2011 11:25 pm

Hi Mic,
As I understand it it is essential 35 copies of the polygon in one object.
Yes, that is the fake I was referring to.

I had forgotten about our emails, looked them up and they were about: Polygons.
Seems an interesting topic. :)

I also see the performance hit, OK, since you accepted the cheat here is a faster version:

Code: Select all

on mouseUp
   put the milliseconds into tStart
   lock screen
   put points of grc P1 into points1
   
   put 35 into tHorzOff
   if tHorzOff < 0 then
      put -1 into tInc
   else
      put 1 into tInc
   end if
   
   put abs (tHorzOff) into tHorzOff
   
   put points1 into newPoints
   put points1 into sOldPoints
   
   repeat tHorzOff
      put "" into tCollect
      repeat for each line aPoint in sOldPoints
         add tInc to item 1 of aPoint
         put aPoint & cr after tCollect
      end repeat
      delete last char of tCollect -- a return
      put tCollect into sOldPoints
      put  cr& cr & tCollect after newPoints
   end repeat
   set points of grc p2 to newpoints
   put the milliseconds - tStart
end mouseUp
brings the computational time down to 2 milliseconds, without screen update. Before it was 590 milliseconds when I used 'move' (also without screenupdate)

Kind regards

Bernd

micmac
Posts: 49
Joined: Mon May 30, 2011 9:00 am

Re: Challenge: Faking 3D extrusion of a polygon

Post by micmac » Thu Jun 02, 2011 10:46 am

Thanks Bernd for your continuing effort


Two things
On my machine I get a report of 1 millisec.
I can sense a definite lag after clicking the button so maybe the report is not true.
Strange enough if I exclude "Lock Screen" the script feels faster. The report is then 446 millisec.


Your script is a special case where the extrusion is in only one direction.
My script (not perfect) can have an extrusion offset of fx 9,17

Thanks
Michael

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4002
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Challenge: Faking 3D extrusion of a polygon

Post by bn » Tue Jun 07, 2011 12:58 pm

Hi Michael,

here is a new version of the 3D extrusion of polygons. It still uses the technique to put multiple polygons into a single graphic object. Livecode lets you do this if you separate the points of the different polygons by an empty line.

This version lets you adjust the offsets x and y by sliders. You can combine x,y offsets.

3dMicMac_0.0.3.livecode.zip
(6.26 KiB) Downloaded 274 times

at times a little jagginess is visible but that is the price of cheating :)
pseudo 3d.png
Have a look at the easter egg...

Kind regards

Bernd

Edit: added an image of what the stack looks like

micmac
Posts: 49
Joined: Mon May 30, 2011 9:00 am

Re: Challenge: Faking 3D extrusion of a polygon

Post by micmac » Thu Jun 09, 2011 7:46 pm

Hi there

Here is a very fast version, with no compromise.

Special thanks goes to Bernd who worked with me offline, and was instrumental to the final solution.

Michael
Fake3D Trapezer.rev.zip
(2.01 KiB) Downloaded 325 times

Post Reply

Return to “Talking LiveCode”