drawing using C

L

liorm

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
 
V

Vladimir Oka

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>
 
V

Vladimir Oka

Vladimir Oka opined:
liorm opined:


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()`.
 
L

liorm

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?
 
L

liorm

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.
 
J

Jordan Abel

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.
 
V

Vladimir Oka

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
the "leading edge" so bad they walk right off the precipice.
(Craig E. Groeschel)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
K

Keith Thompson

liorm said:
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
example, jump to (0, 0), write some characters, jump to (10, 20),
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
thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top