logic code output a shape with string

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

Post Reply
dannykln
Forum Newbie
Posts: 2
Joined: Tue Feb 15, 2011 10:50 pm

logic code output a shape with string

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: logic code output a shape with string

Post by Jonah Bron »

What is the application of this function?
dannykln
Forum Newbie
Posts: 2
Joined: Tue Feb 15, 2011 10:50 pm

Re: logic code output a shape with string

Post by dannykln »

Jonah Bron wrote:What is the application of this function?

nope...it just a test...
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: logic code output a shape with string

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

Re: logic code output a shape with string

Post by Weirdan »

User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: logic code output a shape with string

Post by Jonah Bron »

Could you explain that Weirdan?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: logic code output a shape with string

Post by Weirdan »

Explain the formula, or what?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: logic code output a shape with string

Post by Jonah Bron »

Yeah, I don't see information there that obviously helps...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: logic code output a shape with string

Post by Weirdan »

It gives you the formula for circle, in Cartesian coordinates:
wolframalpha-20110216142705218.gif
wolframalpha-20110216142705218.gif (5.64 KiB) Viewed 283 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 283 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 283 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: logic code output a shape with string

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: logic code output a shape with string

Post by John Cartwright »

<Boston accent>Why I otttaaaaa!!.... properly learn geometry :D</Boston accent>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: logic code output a shape with string

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

Re: logic code output a shape with string

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: logic code output a shape with string

Post 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, '#', '-');
Post Reply