Page 1 of 1

logic code output a shape with string

Posted: Tue Feb 15, 2011 10:54 pm
by dannykln
i have a logic test as below:


write the algorithm to draw a sphere, the function should have parameters
1. diameter (how many line)
2. character (use which character to draw)

eg, draw_sphere(10, '8') will produce:

Code: Select all

                           88888
                       8888888888888    
                    8888888888888888888
                   888888888888888888888
                  88888888888888888888888
                  88888888888888888888888
                   888888888888888888888
                    8888888888888888888
                       8888888888888
                           88888

				
	the above is a sphere of 10 lines, which is drawn using the character "8"
could anyone help me to figure out how to do it? i'm newbie in php , thanks in advanced.

Re: logic code output a shape with string

Posted: Tue Feb 15, 2011 11:32 pm
by Jonah Bron
What is the application of this function?

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 12:09 am
by dannykln
Jonah Bron wrote:What is the application of this function?

nope...it just a test...

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 11:12 am
by Jonah Bron
Hm, that's a tough one. There's two ways to do it: the cheap way, and exponentially grow the number of columns, or the hard/good way, and do the mathematical calculation. The bad part is, I don't know what the formula is. I'll bet pi is involved though.

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 12:03 pm
by Weirdan

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 12:12 pm
by Jonah Bron
Could you explain that Weirdan?

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 12:32 pm
by Weirdan
Explain the formula, or what?

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 12:54 pm
by Jonah Bron
Yeah, I don't see information there that obviously helps...

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 3:25 pm
by Weirdan
It gives you the formula for circle, in Cartesian coordinates:
wolframalpha-20110216142705218.gif
wolframalpha-20110216142705218.gif (5.64 KiB) Viewed 284 times
Incidentally, Cartesian coordinates are also what is used when you draw things on the screen (flipped on Y axis, but that doesn't really matter because the shape you want to draw is symmetrical here).

Looking at the formula it's hard to miss the fact that it's extremely similar to the formula for the distance between two points (Pythagorean theorem):
wolframalpha-20110216150524678.gif
wolframalpha-20110216150524678.gif (5.49 KiB) Viewed 284 times
- which is natural as circle could be defined as a curve equidistant from some fixed point, called center of the circle.

From that it's easy to derive the inequation that each point inside the circle satisfies:
wolframalpha-20110216151734484.gif
wolframalpha-20110216151734484.gif (5.15 KiB) Viewed 284 times
where x1,y1 defines the center point.

Now all you need to do is to iterate over all points on a plane up to x=x1+d/2,y=y1+d/2, for every point check if it's inside the circle using the inequation above and if it is, echo $character;. Otherwise echo ' ';

Add x stretching (because glyphs usually more tall than wide) and you have your drawing function.

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 3:31 pm
by Jonah Bron
:bow: :bow: :bow: :bow: 8O :bow: :bow: :bow: :bow: :bow: :bow: :bow: :bow: :bow: :bow:

Sweet! Now I've got to take a swing at implementing it :) Stay tuned...

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 4:02 pm
by John Cartwright
<Boston accent>Why I otttaaaaa!!.... properly learn geometry :D</Boston accent>

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 4:03 pm
by Jonah Bron
Okay, I got it sort of working, but something's wonky. Check it out here:

http://codepad.org/r6oe30Z0

The formula is correct. Been playing with it for a while, but can't pin down the problem.

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 4:28 pm
by Weirdan
Jonah Bron wrote:Okay, I got it sort of working, but something's wonky.
^ is a bitwise xor operator in php. use pow($x, 2) or $x*$x instead

Re: logic code output a shape with string

Posted: Wed Feb 16, 2011 4:34 pm
by Jonah Bron
Aha. Fixed. Funny, I thought about checking my exponent operator, but said "naw, there's no way it's wrong". :)

http://codepad.org/A8xM8FhT

Code: Select all

function drawCircle($r, $circle, $outside) {
	$output = '';
	
	$x1 = 0;
	$y1 = 0;
	
	$r2 = pow($r, 2);
	
	for ($x = -$r; $x <= $r; $x++) {
		for ($y = -$r; $y <= $r; $y++) {
		
			if ((pow($x - $x1, 2) + pow($y - $y1, 2)) <= $r2) {
				$output .= $circle;
			} else {
				$output .= $outside;
			}
		}
		$output .= PHP_EOL;
	}
	return $output;
}
Usage:

Code: Select all

echo drawCircle(10, '#', '-');