drawing using C

Discussion in 'C Programming' started by liorm, May 2, 2006.

  1. liorm

    liorm Guest

    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
     
    liorm, May 2, 2006
    #1
    1. Advertising

  2. liorm

    Vladimir Oka Guest

    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>
     
    Vladimir Oka, May 2, 2006
    #2
    1. Advertising

  3. liorm

    Vladimir Oka Guest

    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>
     
    Vladimir Oka, May 2, 2006
    #3
  4. liorm

    liorm Guest

    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?
     
    liorm, May 2, 2006
    #4
  5. liorm

    liorm Guest

    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.
     
    liorm, May 2, 2006
    #5
  6. liorm

    Jordan Abel Guest

    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.
     
    Jordan Abel, May 2, 2006
    #6
  7. liorm

    liorm Guest

    Thanks, this is a great idea!
     
    liorm, May 2, 2006
    #7
  8. liorm

    Vladimir Oka Guest

    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>
     
    Vladimir Oka, May 2, 2006
    #8
  9. liorm

    Default User Guest

    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.
     
    Default User, May 2, 2006
    #9
  10. "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) <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.
     
    Keith Thompson, May 3, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos

    Error using system.drawing

    Carlos, Feb 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    403
    Kevin Spencer
    Feb 4, 2004
  2. Richard
    Replies:
    3
    Views:
    4,121
    velocity4anil
    Sep 19, 2007
  3. John via .NET 247

    Problem with using System.Drawing.Bitmap

    John via .NET 247, Aug 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    499
    John via .NET 247
    Aug 5, 2004
  4. Replies:
    1
    Views:
    646
    Kevin Spencer
    Jan 9, 2006
  5. defn noob
    Replies:
    1
    Views:
    403
    Mark Space
    Jun 28, 2008
Loading...

Share This Page