Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > drawing using C

Reply
Thread Tools

drawing using C

 
 
liorm
Guest
Posts: n/a
 
      05-02-2006
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

 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      05-02-2006
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>

 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      05-02-2006
Vladimir Oka opined:

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

 
Reply With Quote
 
liorm
Guest
Posts: n/a
 
      05-02-2006
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?

 
Reply With Quote
 
liorm
Guest
Posts: n/a
 
      05-02-2006
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.

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      05-02-2006
On 2006-05-02, liorm <> 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.
 
Reply With Quote
 
liorm
Guest
Posts: n/a
 
      05-02-2006
Thanks, this is a great idea!

 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      05-02-2006
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>

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-02-2006
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
header.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-02-2006
"liorm" <> 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
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.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Guide to the standard library? Library for drawing in GUIs?drawing inbrowsers? defn noob Java 1 06-28-2008 02:50 AM
System.Drawing: Error when using a .bmp or .gif file but not when .jpg Richard ASP .Net 3 09-19-2007 01:14 PM
funny drawing software:ScreenPen,drawing directly on screen! yyzzbb@sina.com Digital Photography 0 02-04-2006 12:31 AM
System.Drawing For Drawing Text Images jjbutera@hotmail.com ASP .Net 1 01-09-2006 09:55 PM
Error using system.drawing Carlos ASP .Net 1 02-04-2004 10:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57