Page 1 of 2

Math challenge

Posted: Thu Apr 13, 2006 2:56 am
by Chris Corbyn
Equation for a circle (simplified to have center (0,0)) can be given by:

y^2 + x^2 = r^2


Thus:

y = sqrt( r^2 - x^2 )

Let's say we have a circle of radius 10 units:

y = sqrt( 100 - x^2 )

So if we plot points for($x=-10; $x<=10; $x+=0.1) we'd get a circle and we could create a function to get the y values for each value of x.

Problem I'm facing. I can make the circle BUT the points are plotted equidistantly on a horizontal line, not equidistantly as you travel around the perimiter of the circle. What I need to do is to ensure that all of the points around the edge of the circle are equally spaced.

http://www.iris.ac/~cac/gtest2.php (wrong spacing)

One way I can think of is to determine the appropriate X values to use before-hand.

Is anybody enough of a maths wizard to be able to determine a function that provides an appropriate sequence of numbers along the X axis that result in those dots in my example all being equally spaced out?

Posted: Thu Apr 13, 2006 3:00 am
by Ree
Sorry for not being much of help, but I'd like to mention your script makes my FF lag like hell.

Posted: Thu Apr 13, 2006 3:14 am
by Chris Corbyn
Ree wrote:Sorry for not being much of help, but I'd like to mention your script makes my FF lag like hell.
Yeah I'm just mucking about right now. Sounds like you tried it when I had it with a color fill though :P

Posted: Thu Apr 13, 2006 3:18 am
by Chris Corbyn
Actually I could use trigonometry instead and incremeny n degrees each iteration.

Posted: Thu Apr 13, 2006 3:55 am
by Chris Corbyn
Image

cos(theta) = a / h
a = cos(theta) * h

So for gaps of 5 degrees:

a = cos(5) * 20px

I always knew that trig stuff we did at school would be used one day :P

Posted: Thu Apr 13, 2006 4:41 am
by Chris Corbyn
If anyone's interested here's a working version:

http://www.iris.ac/~cac/gtest3.php

Posted: Thu Apr 13, 2006 5:57 am
by timvw
Congratz ;)


(What was i thinking when i thought that xhtml + css would make and end to the table-tag-soup)

Posted: Thu Apr 13, 2006 6:01 am
by onion2k
Why not generate an image?

Posted: Thu Apr 13, 2006 7:18 am
by Chris Corbyn
onion2k wrote:Why not generate an image?
Because this is more fun :P

Posted: Thu Apr 13, 2006 11:06 am
by onion2k
d11wtq wrote:
onion2k wrote:Why not generate an image?
Because this is more fun :P
Pfft. Nothing is more fun than mucking about with GD.

Well, a few things are, but they're nothing to do with PHP.

Posted: Thu Apr 13, 2006 11:42 am
by Chris Corbyn
onion2k wrote:
d11wtq wrote:
onion2k wrote:Why not generate an image?
Because this is more fun :P
Pfft. Nothing is more fun than mucking about with GD.

Well, a few things are, but they're nothing to do with PHP.
Nah, to answer your question seriously I'm writing a graphing library in PHP with the goal of providing something similar to JPGraph (Roja has helped out/is helping out on the current version) except that it will NOT depend on GD or anything other than a vanilla PHP installation. It's all CSS based.

The current version only did Bar Graphs... this one will do any number of things. This was for a pie chart. Yes yes I know the markup is huge but I have ideas on ways to bring that down in size... I just needed the basic maths worked out first ;) But I do also love just doing fresh things with CSS.

Posted: Thu Apr 13, 2006 3:24 pm
by Christopher
There are a number of DHTML drawing libraries. The Walter Zorn one is probably the oldest here. If you Google "javascript drawing library" you can find a bunch.

You should especially look at PlotKit which does all kinds of graphs using either Javascript or SVG. Very cool. It's built on Mochikit.

Posted: Fri Apr 14, 2006 4:11 am
by Weirdan
d11wtq wrote: Nah, to answer your question seriously I'm writing a graphing library in PHP with the goal of providing something similar to JPGraph (Roja has helped out/is helping out on the current version) except that it will NOT depend on GD or anything other than a vanilla PHP installation. It's all CSS based.
Haven't you considered to use canvas instead of html+css?

Posted: Fri Apr 14, 2006 9:51 am
by Ambush Commander
But canvas has limited browser support, last I checked.

Posted: Fri Apr 14, 2006 10:18 am
by Weirdan
there are efforts to implement it for IE. Opera, Safari & Mozilla support canvas out of the box.