473,849 Members | 1,992 Online

# drawing using C

Hi folks,

I need to write a short program which gets a few parameters and draws a
diagram in the text file. Basically what I need is a guidance on how I
put stream indicator to the required position. What I mean is that I
need to draw a character at the (x,y) coordinate. But how do I bring
the indicator to this position. I tried to use fsetpos(fp, y*80+x) but
then when I draw the character by fputs("_",fp) I see it on the first
line with y*80+x offset instead of y-th line with x offset. I'd
appriciate any idea. Thanks.

LM

May 2 '06 #1
9 1871
liorm opined:
Hi folks,

I need to write a short program which gets a few parameters and draws
a diagram in the text file. Basically what I need is a guidance on
how I put stream indicator to the required position. What I mean is
that I need to draw a character at the (x,y) coordinate. But how do I
bring the indicator to this position. I tried to use fsetpos(fp,
y*80+x) but then when I draw the character by fputs("_",fp) I see it
on the first line with y*80+x offset instead of y-th line with x
offset. I'd appriciate any idea. Thanks.

If you can draw your diagram sideways (i.e. make it YX not XY) then
every line is for one X, and you can put a character (say an 'o') Y
spaces away from the beginning of the line. Obviously, you can also
have axes, labels etc. For example, Y = X could look like:

-5 0 5
--------------------o--------------------> Y
|o
| o
| o
| o
| o
| o
| o
V
X

The only things you need would be basic `printf()` et al.

--
I could dance till the cows come home. On second thought, I'd rather
dance with the cows till you come home.
-- Groucho Marx

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

May 2 '06 #2
liorm opined:
Hi folks,

I need to write a short program which gets a few parameters and
draws a diagram in the text file. Basically what I need is a
guidance on how I put stream indicator to the required position.
What I mean is that I need to draw a character at the (x,y)
coordinate. But how do I bring the indicator to this position. I
tried to use fsetpos(fp, y*80+x) but then when I draw the character
by fputs("_",fp) I see it on the first line with y*80+x offset
instead of y-th line with x offset. I'd appriciate any idea. Thanks.

If you can draw your diagram sideways (i.e. make it YX not XY) then
every line is for one X, and you can put a character (say an 'o') Y
spaces away from the beginning of the line. Obviously, you can also
have axes, labels etc. For example, Y = X could look like:

-5 0 5
--------------------o--------------------> Y
|o
| o
| o
| o
| o
| o
| o
V
X

The only things you need would be basic `printf()` et al.

OK, I may have not been clear enough. Here's some meta-code:

You go through all Xs in turn, one line dedicated to each. You
calculate corresponding Y. A marker ('o') is put after N spaces, where
N corresponds to Y (depending on desired scale). You can decorate with
labels and/or axes to taste. All you need are loops for Xs,
calculation of Ys and Ns, and a `printf()`.
--
Fortunate is he for whom the belle toils.

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

May 2 '06 #3
I don't really get how this helps me. I still need to know how to bring
myself to a certain coordinate, whether it is (X,Y) or (Y,X). As I
mentioned above I tried fsetpos and fseek, but both of them leave me on
the first line of the output file. How can I skip lines to get to the
required coordinate?

May 2 '06 #4
Thanks.
I don't really get how this helps me. I still need to know how ito
bring myself to the required coordinate, whether it is (X,Y) or (Y,X).
As I mentioned above I tried fsetpos and fseek but both of them left me
on the first line of the text file, while I need to skip lines to reach
the coordinate.

May 2 '06 #5
On 2006-05-02, liorm <li***@ti.com > wrote:
Thanks.
I don't really get how this helps me. I still need to know how ito
bring myself to the required coordinate, whether it is (X,Y) or (Y,X).
As I mentioned above I tried fsetpos and fseek but both of them left me
on the first line of the text file, while I need to skip lines to reach
the coordinate.

You misunderstand how text files work.

Probably the easiest thing to do would be to draw it in an array of char
arrays in memory and then print it out.
May 2 '06 #6
Thanks, this is a great idea!

May 2 '06 #7
liorm opined:

You need to quote some context (what was said, and by whom).
Thanks.
I don't really get how this helps me. I still need to know how ito
bring myself to the required coordinate, whether it is (X,Y) or
(Y,X). As I mentioned above I tried fsetpos and fseek but both of
them left me on the first line of the text file, while I need to skip
lines to reach the coordinate.

Let's say you want to plot Y = 2*X for X from 0 to 4. You can do that
by printing one line to a text file representing Y position on the
graph that corresponds to X=0, then the next for X=1, and so forth.

X Y Output
0 0 "o"
1 2 " o"
2 4 " o"
3 6 " o"
4 8 " o"

Catch my drift? You can output leading blanks in a loop:

for (X = 0; X < 5; ++X) {
Y = 2 * X;
for (n = 0; n < (Y-1); ++n)
printf(" ");
printf("o\n");
}

would do the trick for any given Y (you may want you use `fprintf()`
unless it's OK to just redirect `stdout` to a file).

As I said, you can make it as fancy as you wish, with scaling, labels,
etc.

As Jordan pointed out, you seem to have a misconception about text
files. They are not grids that you can fill in to taste. You have to
build them line by line.

--
Sigh. I like to think it's just the Linux people who want to be on
(Craig E. Groeschel)

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

May 2 '06 #8
liorm wrote:
Thanks, this is a great idea!

See below.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
May 2 '06 #9
"liorm" <li***@ti.com > writes:
I need to write a short program which gets a few parameters and draws a
diagram in the text file. Basically what I need is a guidance on how I
put stream indicator to the required position. What I mean is that I
need to draw a character at the (x,y) coordinate. But how do I bring
the indicator to this position. I tried to use fsetpos(fp, y*80+x) but
then when I draw the character by fputs("_",fp) I see it on the first
line with y*80+x offset instead of y-th line with x offset. I'd
appriciate any idea. Thanks.

It's possible on many systems to control the cursor position (for
write some more characters, etc.). There's just no way to do this in
standard C. If you want this kind of control, as used by a
full-screen text editor, look for "curses", "ncurses", or something
similar.

But if you want the diagram in a text file rather than on a display,
this doesn't help you. For that, see the other replies in this

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.