Re: Hints on hoq to create this graph?
by zentara (Cardinal) on Feb 13, 2014 at 15:49 UTC
|
This smells like a homework question, so I will give you a clue. Look at how the following works.
You will need to do a trigonometric calculation for each line, seeing where it interesects with your various circles. See Circle line intersection
But you could just wing it, and start printing characters by trial and error, till you get a circle. But thats your homework, not mine. :-) You might want to google for ascii art bullseye. Also, if you understand the XPM file format, you can create intricate xpm graphics with your character set. You can convert XPM's to jpg later if you wanted.
This is actually a left arrow, if seen from a terminal.
my $left = <<"EOD";
/* XPM */
static char * pixmap[] = {
/* width height num_colors chars_per_pixel */
" 16 12 3 1 ",
/* colors */
" s None c None",
". c black",
"X c yellow",
/* pixels */
"..........X.....",
"........XXX.....",
"......XXXXX.....",
"....XXXXXXX.....",
"..XXXXXXXXX.....",
"XXXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXXX",
"..XXXXXXXXX.....",
"....XXXXXXX.....",
"......XXXXX.....",
"........XXX.....",
"..........X....."};
EOD
| [reply] [d/l] |
Re: Hints on hoq to create this graph?
by davido (Cardinal) on Feb 13, 2014 at 17:15 UTC
|
use strict;
use warnings;
Don't think of this as "How am I going to use the commands available to me to do this?" Instead, think "How would I do this with a pen and a sheet of graph paper?" Then use the commands available to you to implement that human algorithm as a computer algorithm.
For example; you would start by laying a sheet of graph paper before you, determining its cell width, and then placing a mark in the top middle square. Then you would move down one row, and place three marks, starting one space to the left of middle. Depending on how "round" you want this bull's eye to be, and how much detail, you would adjust how many spaces to the left to start on each new row, and you would adjust how many marks to place. You might even adjust what type of mark you place. For each new row you repeat this decision making process and marking process. At some point you find yourself halfway done, and begin reversing the process so that each row has fewer marks placed in it, and starts a little closer to the middle. Finally you end near the bottom of the page with a single mark at the middle.
So as you're doing this with pen and graph paper, you are keeping track in your mind where the last row started and finished, so that you can calculate where the next row starts and finishes. You are also keeping track how many rows you have completed so that you know when to begin reducing the width instead of increasing it for each row.
On a computer you would do the same thing; a loop would be used to iterate over the rows, and calculations would be made on each row based on the criteria of where you were in the last row, and how many rows have been completed. You would use that calculation to decide how many marks to place, and where. All of this looping, logic, and calculation can be accomplished with "for" or "while" loops, "if" statements, mathematical expressions, and variables for storing state.
So get started. If you haven't done so already, read through perlintro first. But then, really, get started and see how far you get. You gain nothing by us solving it for you, but you gain a lot by trying yourself, and possibly asking us for clarification on sticking points.
| [reply] [d/l] |
Re: Hints on how to create this graph?
by roboticus (Chancellor) on Feb 13, 2014 at 17:00 UTC
|
I'd approach it something like this:
First, divide your space into cells (each one will hold one character position).
Next, decide the thickness of your line you use to draw the circle.
To draw the circle, I'd draw the outermost circle (the one associated with the "outside" of your line) then subtract out your inner circle.
Finally, any cell completely inside or outside of your circle is easy: it's blank. Any cell completely within the two circles is similarly easy: it's as dark as possible (#). The intermediate values would depend on how much of the cell lies outside of the line.
Since your picture is a simple circle, all you need to do is compute the percentage of area obscured by your circle (from 0 to 100%) and the assign the "color" (i.e. ' ', '.', '+', '*', '#') based on the percentage.
Something like:
- Build array and set to 0 everywhere
- For each row
- For each column
- If cell totally outside of outer circle, cell=0
- Otherwise if cell is totally inside the inner circle, cell=0
- Otherwise if cell is totally within the two circles, cell=100
- Otherwise:
- Compute fraction of cell within the outer circle
- Subtract fraction of cell within the inner circle
- Look up character to print for the cell, based on the value
- print the character
I hope this is helpful, but doing too much of your work for you.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
| [reply] |
Re: Hints on hoq to create this graph?
by kennethk (Abbot) on Feb 13, 2014 at 15:58 UTC
|
As ww has already pointed out, the spare nature of your post does not help the monks here know how to be helpful. For example:
What is a '"bulls-eye" graph'? How do characters like '.', '+', '*', and '#' map to underlying values? Can you provide us with an example dataset and what the associated chart would look like (wrapped in <code> tags)?
How much experience with Perl do you have? Is this your first task ever? Are you expert in another language, and just unfamiliar with Perl syntax?
Is this homework? Is this maintaining some esoteric script left by a former employee? Is this a random assignment from a higher up who heard that this Perl thing is pretty useful? Have terrorists kidnapped your daughter, demanding code in exchange for her safe release? Context is important in terms of what resources will be most useful for you in the long run.
Take a step back and read On Asking Questions of Bears. If you can provide us with an algorithm, we can help you translate that into code.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
| [reply] [d/l] |
Re: Hints on hoq to create this graph?
by zentara (Cardinal) on Feb 13, 2014 at 19:07 UTC
|
Well, just in case this problem was assigned to you as part of your Phd. thesis research, :-), I would say, that for a bullseye, switch to a radial coordinate system.
So set up a grid to print, say an array of arrays, AOA's. Say you had an array size of 80x80, for array indices 0..79.
So at (40,40) you would setup your origin for your radial coordinate system, where every position is defined by a reference angle and a radial distance to the object.
So, start filling in your array blocks with your characters, starting at the center, and blossoming outwards. So you determine at that any given position what the radius is to that cell, and if the radius is in certain ranges, they get assigned different characters to that cell.
But, I won't say more, as it might influence you too much. The radius can be easily converted to cartesian coordinates with some simple trig.
P.S. For mucho bonus points, how might you implement a 3 dimensional bullseye?
| [reply] |
Re: Hints on how to create this graph?
by davies (Monsignor) on Feb 13, 2014 at 17:12 UTC
|
| [reply] |
Re: Hints on hoq to create this graph?
by ww (Archbishop) on Feb 13, 2014 at 15:40 UTC
|
The closing code tag must be "</code>" and because you read -- at least in part - the instructions on how to format a question, you do get some (IMHO) useful advice:
Non-specific questions that make no effort to show that you've actually worked at coming up with an answer -- even if that work has failed -- are apt to get only generic answers... like:
- perldoc perldoc
- Tutorials
- Learning Perl (O'Reilly - pretty much any edition > 2)
- ...etc ...etc ...etc.
| [reply] |
Re: Hints on hoq to create this graph?
by choroba (Cardinal) on Feb 13, 2014 at 22:10 UTC
|
The main inequation is $x ** 2 + $y ** 2 < $r ** 2, which describes which points x, y in a grid are part of a circle of the radius r. An obfuscated example of the solution is below:
| [reply] [d/l] [select] |
|
|
Thank you choroba,
I am a bit confused as to what you do in your sub-routine...
Could you maybe explain this a bit?
sub p
{
my($w,$v)=@_;
for my $e(1..$#chars_all)
{
if($w**2+$v**2<($e*20/$#chars_all)**2)
{return $chars_all[$e]}
}' '
}
Particularly, I can't understand the }' ' part that you have...
Thank you very much! | [reply] [d/l] [select] |
|
|
If you are too far from the centre, the subroutine returns a space.
| [reply] |
Re: Hints on hoq to create this graph?
by tangent (Parson) on Feb 13, 2014 at 16:00 UTC
|
For visually controlling output, have a look at printf | [reply] |
|
|
print
" *****\n".
" * *\n".
" * O *\n".
" * O *\n".
" * *\n".
" *****\n";
Rata (assuming the OP has some hidden requirements not posted ... )
update:
Based on the commands available to you, you teacher probably expects you to use an array (of the size (2n+1)(2n+1)), with (n)(n) being the center. Then you should determine the distance from each field to the center (using the pythargoras-formula - that's why you are allowed to use sqrt), and decide based on that distance which character to use for that element - that way you'll get circles.
Have fun trying! | [reply] [d/l] [select] |