8 Dimensions Rendered

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

8 Dimensions Rendered

Post by tentus »

The title sounds far more grand than what I'm actually doing. In a nutshell, my dad asked me to visualize an eight-dimensional system, where each dimension of the system was an aspect of ethical principles. He wanted to know how many possible combinations are there, and wanted me to make him a rendering of them in an octagonal arrangement.

My older brother beat me to the punch and did a math paper on this stuff, and even wrote a cool little program to output SVGs of each possibility. By his math, there are 268,435,456 possibilities (buh!). You can see the paper here: http://wjholden.com/octagon/

Now, I've finally gotten around to writing a .love that can render the general idea. If you guys could look at the source and make suggestions that would be grand.

Controls:
Enter: toggle text
Space: pause
Up: Increase odds of showing line
Down: Decrease odds of showing line
Escape: Exit
Attachments
8dimensions.love
Version 3.
(1.58 KiB) Downloaded 367 times
8dimensions.love
Version 2
(1.4 KiB) Downloaded 296 times
8dimensions.love
Version 1.
(1.39 KiB) Downloaded 284 times
Last edited by tentus on Tue Jul 19, 2011 3:46 am, edited 3 times in total.
Kurosuke needs beta testers
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 8 Dimensions Rendered

Post by ivan »

Hey, I looked at your script code.
It's pretty clean and looks farily bug-free.
There are a few optimization tricks you may want to consider:

Code: Select all

	for i=1, 8 do
		for j=1, 8 do
			if points[i][j] == 1 then
Lua has an easier (and faster) way of implementing those loops:

Code: Select all

for i, v in pairs (points) do
Yes, 'pairs' is generally faster than numeric loops (and faster than 'ipairs' too).

Code: Select all

function changePoints()
	for i=1, 8 do
		points[i] = {0,0,0,0,0,0,0,0}	-- zero out old values
This code is not terribly efficient in particular.
It's executed every frame and you are allocating a new table each time.
This is doubly slow since you are overwriting your reference to the old table which the garbage collector needs to pick up.
A faster alternative would be:

Code: Select all

-- zero out old values
for j, v in pairs (points[i]) do
  points[i][j] = 0
end
I'm not too crazy about the "getCoords" function.
I think it could be simplified using trigonometry.
Something like:

Code: Select all

function getCoords(p)
  local a = math.rad(45)*p
  local x, y = math.cos(a), math.sin(a)
  return x * 150 + 250, y * 150 + 250
end
Where 150 is the 'radius' of your octagon and 250 is the offset where you want it rendered.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: 8 Dimensions Rendered

Post by Robin »

ivan wrote:Yes, 'pairs' is generally faster than numeric loops (and faster than 'ipairs' too).
What. Have you got evidence for that? I've always heard that it was the other way, and it makes sense too.
Help us help you: attach a .love.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: 8 Dimensions Rendered

Post by thelinx »

Robin wrote:
ivan wrote:Yes, 'pairs' is generally faster than numeric loops (and faster than 'ipairs' too).
What. Have you got evidence for that? I've always heard that it was the other way, and it makes sense too.
Well, there's this.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: 8 Dimensions Rendered

Post by Robin »

thelinx wrote:Well, there's this.
Interestingly, that same analysis seems to indicate that i=1,# is much faster than either (scroll down).
Help us help you: attach a .love.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: 8 Dimensions Rendered

Post by tentus »

I implemented ivan's new getCoords() function, seems to be working just fine: thanks!

Also rearranged how some of the code works, now it generates a table of coords whenever the render changes, and the render pulls from that, rather than recalculating each frame. I also added a simple frame capping statement:

Code: Select all

	if dt < (1/60) then
		love.timer.sleep((1/60) - dt)
	end
Interestingly, I cannot discern a performance difference between the two programs. Any input, guys?
Kurosuke needs beta testers
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: 8 Dimensions Rendered

Post by Kadoba »

Wait yeah, it's just 2^7! isn't it?

You should be able to figure any n value system by 2^(n-1)!

I think. I suck at math.

Edit:
Argh, nevermind. I got summations and factorials mixed up.

I told you I suck at math.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: 8 Dimensions Rendered

Post by tentus »

Apparently this runs super fast on an Asus laptop, when merged into an exe (via 0.7.2). Is anyone aware of any dt problems in merged games?
Kurosuke needs beta testers
User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: 8 Dimensions Rendered

Post by ishkabible »

about the ipairs vs. pairs vs. generic for loop, it makes sense that for loops would be so much faster for a few reasons

* no function calls yet Lua has instructions MADE for this in it's VM instructions
* the expressions doesn't need to be evaluated every time as in a while loop, it's only evaluated once then i is compared to it

Code: Select all

local t = {1,2,3}

for i=0, #t, 1 do
	print(i)
	table.insert(t, i)
end
* no conversions from the lua API when switching functions or extra copys, everything stays in place and nothing gives controls to another function
* function calls carry a significant tole in performance even with languages like C/C++
* numbers are faster to use than generators

it makes sense that these would be faster, it seems odd that ipairs isn't a little bit faster though sense it dosn't have to look at the hash table part only the array part.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: 8 Dimensions Rendered

Post by bartbes »

Of course this is tested on equal table structure, or at least I'd hope so. As for why that makes pairs faster, it is unordered, so it doesn't have to make sure things are in order, as ipairs does.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests