Math challenge

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Math challenge

Post 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?
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Sorry for not being much of help, but I'd like to mention your script makes my FF lag like hell.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Actually I could use trigonometry instead and incremeny n degrees each iteration.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

If anyone's interested here's a working version:

http://www.iris.ac/~cac/gtest3.php
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Congratz ;)


(What was i thinking when i thought that xhtml + css would make and end to the table-tag-soup)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Why not generate an image?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

onion2k wrote:Why not generate an image?
Because this is more fun :P
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

But canvas has limited browser support, last I checked.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

there are efforts to implement it for IE. Opera, Safari & Mozilla support canvas out of the box.
Post Reply