Thread: Making a teapot using triangles.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    Making a teapot using triangles.

    Hi.

    I've just received an assignment in c programming that I honestly have no clue on how to solve. It's due in two weeks, so I'm hoping hard work can get me through this with full understanding of the subject.

    I've attached the files here, the assignment text and the files we have received. I'm not too sure how I should upload everything here, as there are several restrictions, but I might have to make a new post for the rest of the files. I tried attaching the zip file, but it was too large (427,7kb) - any suggestions how I can add this?

    What I'm looking to achieve with this forum post is help and guidelines as to where to start, in which order I should go by to finish this assignment and how to write codes etc. I want to learn how to do this, and that's my main focus, rather than just get the assignment done and deliver it without fully understanding the code.

    Any input regarding this post and attachments are appreciated as well, and obviously any input what so ever regarding my assignment are greatly appreciated.
    Attached Images Attached Images Making a teapot using triangles.-exercise-part-1-jpg Making a teapot using triangles.-exercise-part-2-jpg 
    Last edited by LEL; 10-03-2013 at 06:58 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,920
    Read the Homework rules! Announcements - General Programming Boards

    Do some work; then ask for help!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Thankfully, we may not need your 400MB zip file. (Although one way to give it to others is to post the link where you got it yourself.) Since the difficult work is done by the functions given to you, the project should be fairly straightforward. But you have to make the attempt yourself. Start with DrawTriangle. What have you tried?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by stahta01 View Post
    Read the Homework rules! Announcements - General Programming Boards

    Do some work; then ask for help!

    Tim S.
    Obviously! My intentions are by no means to come in here and ask for someone to solve the problem for me.

    However, I'm wondering how I can attach my zip file to give you the full understanding of my assignment. Also, I'm not too sure how to start as this assignment is far beyond what I feel we've learned so far. That's why I'm starting early, and my intentions are to learn how to write this code and understand it fully with your guidelines and help.

    I've started by just drawing some triangles to get somewhat used to the code itself, but to show you this, I need to attach the zip file, so I can update you with what I've done by only attach the .c file I'm editing. Let me know if I've explained this unclear.

    My question now in terms of the code is how I can scale my triangles and keeping the center point, because I think this is the next step towards finishing this teapot.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    If you're homework problems are of this size/complexity, you should be able to at least get a start on your own. You don't even really have a programming issue yet. It's a problem solving and thought organization issue.

    You definitely don't need to attach all the code you were given -- not unless it becomes relevant to solving an actual programming problem, and even then, only post what is needed.

    The first thing with any problem is to understand what problem you are trying to solve. What is the goal here? Once you understand that, you should start thinking about how to solve that problem. Think abstractly, as though there is no computer/programming involved. Get out some trusty pencil and paper. You must understand how to solve the problem yourself before you can tell a computer how to solve it for you.

    Once you have that down, it's time to start turning that abstract solution into something programming-ish. This is usually something like flow charts, pseudo code, etc. Flesh out your design at that stage. Once you're all done there, you can start coding. Work in small chunks (5-10 lines), compile it (with maximum warning level) and fix all warnings and errors. Then test your code. Don't move on until that section is done and working. It's much easier to find a bug that way, then looking through 1000 lines of code that you typed up on a whim and finally decided to "try out". Note, 5-10 lines seems tedious, but as you become a better programmer, that will increase.

    The instructions actually are very thorough and give you lots of details about what each function should do, it's signature, etc. So pick a function and figure out (again, abstractly) how you would do that. Then, design, code and test. Once you're done, move onto the next function. Note that the instructions actually suggest a rough order to write the functions in -- maybe, just maybe, that's a good suggestion.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by oogabooga View Post
    Thankfully, we may not need your 400MB zip file. (Although one way to give it to others is to post the link where you got it yourself.) Since the difficult work is done by the functions given to you, the project should be fairly straightforward. But you have to make the attempt yourself. Start with DrawTriangle. What have you tried?
    Alright. I thought you needed the makefile etc to understand my assigment. I've never ever even seen a programming... program before until we started this subject a month ago. I've learned quite a bit I feel, but still nowhere near enough to complete this assignment on my own without any help. Right now I feel that the whole file is a mess.

    I've started with drawing some triangles, and I've attached that file here. I don't know if that tells you anything, but I've written the lines 88-90.

    Now I want to try and scale this by keeping the center point, and that's what I'll work on. I guess I'll write a new post when I have some progress or if I'm stuck

    Thanks for all the replies so far. Still learning a lot, obviously.

    Edit:

    Anduril: I've actually done this. I have a basic idea of how I want the bow around the triangles to be, but I actually have no idea of what functions I should use in the code.

    I will, however, read up a bit more and try to figure that out, but as you said, I'll finish what I'm doing first. Drawing triangles so I at least have something showing up in my 'app box', that's now done. Now I'll attempt to scale this while the center point remains. I'll spend more time reading and trying to understand the assignment properly though. I still have a lot of time left and I'll use it wisely and work hard towards completing this.

    Thanks a lot for your input!
    Attached Files Attached Files
    Last edited by LEL; 10-03-2013 at 08:08 AM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by LEL View Post
    but I actually have no idea of what functions I should use in the code.
    Well...
    Quote Originally Posted by The instructions you posted
    In this assignment there should be no need to define functions beyond those already defined in the files we provide.

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by anduril462 View Post
    Well...
    Hehe, I'm well prepared to look like an idiot the first few days here. Bear with me! I don't even understand what I'm saying.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You could post these files: drawline.h, drawline.c, triangle.h, teapot_data.h, main.c.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Thanks for your input.

    The teapot_data.h file is 101.9kb, and the forum limit is 97.7kb for that file, I'm sorry.
    Attached Files Attached Files

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The teapot_data.h file is 101.9kb, and the forum limit is 97.7kb for that file, I'm sorry.

    Can you post, say, 10K or it?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by oogabooga View Post
    [/COLOR]Can you post, say, 10K or it?
    I don't know if I can edit it somehow, but it just gives me an error message saying the file is too large.

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You could compile and run this code in the same directory as teapot_data.h and then post teapot_data2.h.
    Code:
    #include <stdio.h>
    int main(void)
    {
        FILE *fin = fopen("teapot_data.h", "rb");
        FILE *fout = fopen("teapot_data2.h", "wb");
        int i;
        for (i = 0; i < 5000; i++) // 5000 bytes is enough!
            fputc(fgetc(fin), fout);
        fclose(fout);
        fclose(fin);
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by oogabooga View Post
    You could compile and run this code in the same directory as teapot_data.h and then post teapot_data2.h.
    Code:
    #include <stdio.h>
    int main(void)
    {
        FILE *fin = fopen("teapot_data.h", "rb");
        FILE *fout = fopen("teapot_data2.h", "wb");
        int i;
        for (i = 0; i < 5000; i++) // 5000 bytes is enough!
            fputc(fgetc(fin), fout);
        fclose(fout);
        fclose(fin);
        return 0;
    }
    I'm amazed.... File attached. Let me know if that's enough. If not I can, as you most likely are aware of, attach a file up to 97.7kb or so.

    Thanks a lot for showing interest in this problem. Gives me hope of completing it withing a couple of weeks. Really appreciate it.
    Attached Files Attached Files

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    For future reference, here's the relevant information in a single post without attachments.

    The teapot data is a version of freeglut_teapot_data.h. Below is the first couple of triangles. The scale member is apparently 1.0 for all of them.

    teapot_data.h
    Code:
    #define TEAPOT_NUMTRIANGLES    1092
    triangle_t teapot_model[] = {
    {
    .x1=-160,
    .y1=-142,
    .x2=-160,
    .y2=-142,
    .x3=-199,
    .y3=-142,
    .fillcolor=0x41a7,
    .scale=1.0
    },
    {
    .x1=-160,
    .y1=-142,
    .x2=-199,
    .y2=-142,
    .x3=-176,
    .y3=-128,
    .fillcolor=0xd63af1,
    .scale=1.0
    },

    drawline.h
    Code:
    #ifndef DRAWLINE_H_
    #define DRAWLINE_H_
    
    // Draw a line on the screen from x1,y1 to x2,y2 with a given pen color
    void DrawLine(SDL_Surface *screen, int x1, int y1, int x2, int y2, unsigned int color);
    
    // Read pixel x,y on the screen
    unsigned int GetPixel(SDL_Surface *screen, int x, int y);
    
    // Set pixel x,y on the screen
    void SetPixel(SDL_Surface *screen, int x, int y, unsigned int color);
    
    #endif /* DRAWLINE_H_ */

    triangle.h
    Code:
    #ifndef TRIANGLE_H_
    #define TRIANGLE_H_
    
    typedef struct triangle triangle_t;
    
    struct triangle {
        // Model coordinates
        int x1, y1;
        int x2, y2;
        int x3, y3;
    
        // Fill color
        unsigned int fillcolor;
        
        // Scale factor
        float scale;
    
        // Translate to these coordinates before drawing on screen
        int tx, ty;
        
        // Bounding box of on-screen coordinates
        int bx, by;         // upper left corner of box
        int bw, bh;         // Width and height of box
        
        // On-screen coordinates
        int sx1, sy1;
        int sx2, sy2;
        int sx3, sy3;
    };
    
    // Draw triangle on screen.
    void DrawTriangle(SDL_Surface *screen, triangle_t *triangle);
    
    #endif /*TRIANGLE_H_*/

    triangle.c
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include "SDL.h"
    #include "triangle.h"
    #include "drawline.h"
    
    #define TRIANGLE_PENCOLOR   0xBBBB0000
    
    
    // Print triangle coordinates along with a message
    void PrintTriangle(triangle_t *triangle, char *msg)
    {
        printf("%s: %d,%d - %d,%d - %d,%d\n",
            msg,
            triangle->x1, triangle->y1, 
            triangle->x2, triangle->y2, 
            triangle->x3, triangle->y3);
    }
    
    
    // Return 0 if triangle coordinates are outside the screen boundary. 1 otherwise.
    int SanityCheckTriangle(SDL_Surface *screen, triangle_t *triangle)
    {
        if (triangle->sx1 < 0 || triangle->sx1 >= screen->w ||
            triangle->sx2 < 0 || triangle->sx2 >= screen->w ||
            triangle->sx3 < 0 || triangle->sx3 >= screen->w ||
            triangle->sy1 < 0 || triangle->sy1 >= screen->h ||
            triangle->sy2 < 0 || triangle->sy2 >= screen->h ||
            triangle->sy3 < 0 || triangle->sy3 >= screen->h) {
            return 0;
        } else {
            return 1;
        }
    }
    
    
    // Scale triangle
    void ScaleTriangle(triangle_t *triangle)
    {
        // TODO: Replace the code below with code that scales each triangle coordinate. 
        // The scaling factor is specified in triangle->scale.
        // Remember that this function MUST write to the on-screen coordinates.
        // Do not alter the model coordinates.
        
        triangle->sx1 = triangle->x1;
        triangle->sx2 = triangle->x2;
        triangle->sx3 = triangle->x3;
        triangle->sy1 = triangle->y1;
        triangle->sy2 = triangle->y2;
        triangle->sy3 = triangle->y3;
    }
    
    
    // Move triangle to its screen position
    void TranslateTriangle(triangle_t *triangle)
    {
        // TODO: Insert code that moves the triangle on the screen.
        // The translation coordinates are specified in triangle->tx and triangle->ty.
        // Remember to use the on-screen coordinates (triangle->sx1, etc.)
        
    }
    
    
    // Calculate triangle bounding box
    void CalculateTriangleBoundingBox(triangle_t *triangle)
    {
        // TODO: Insert code that calculates the bounding box of a triangle.
        // Remember to use the on-screen coordinates (triangle->sx1, etc.)
        // The bounding box coordinates should be written to 
        // triangle->bx, triangle->by, triangle->bw, triangle->bh
    }
    
    
    // Fill triangle with a color
    void FillTriangle(SDL_Surface *screen, triangle_t *triangle)
    {
        // TODO: Insert code that fills the triangle with the color specified in triangle->fillcolor.
        // Hint: Draw the triangle with color TRIANGLE_PENCOLOR (this color can not
        // occur in e.g. the teapot or the example triangles).  Thus, if your 
        // approach to filling the triangle relies on looking for the edges of
        // the triangle on the screen (via the GetPixel function), you will find those
        // edges even if the triangle overlaps with a triangle that has already
        // been drawn on the screen.
    }
    
    
    // Draw triangle on screen
    void DrawTriangle(SDL_Surface *screen, triangle_t *triangle)
    {
        int isOK;
        
        // Scale.
        ScaleTriangle(triangle);
        
        // Translate.
        TranslateTriangle(triangle);
        
        // Determine bounding box
        CalculateTriangleBoundingBox(triangle);
    
        // Sanity check that triangle is within screen boundaries.
        isOK = SanityCheckTriangle(screen, triangle);
        if (isOK == 0) {
            PrintTriangle(triangle, "Triangle outside screen boundaries");
            return;
        }
    
        // TODO: Insert calls to DrawLine to draw the triangle.
        // Remember to use the on-screen coordinates (triangle->sx1, etc.)
    
    // The following 3 lines are the OP's code so far.
        DrawLine(screen, triangle->sx1, triangle->sy1, triangle->sx2, triangle->sy2, TRIANGLE_PENCOLOR);
        DrawLine(screen, triangle->sx2, triangle->sy2, triangle->sx3, triangle->sy3, TRIANGLE_PENCOLOR);
        DrawLine(screen, triangle->sx1, triangle->sy1, triangle->sx3, triangle->sy3, TRIANGLE_PENCOLOR);
    
        // Fill triangle
        FillTriangle(screen, triangle);
    
        // Force screen update.  
        //SDL_UpdateRect(screen, triangle->bx, triangle->by, triangle->bw, triangle->bh);
    
        // Force update of entire screen.  Comment/remove this call and uncomment the above call
        // when your CalculateTriangleBoundingBox function has been implemented.
        SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
    }

    main.c
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include "SDL.h"
    #include "drawline.h"
    #include "triangle.h"
    #include "teapot_data.h"
    
    triangle_t exampletriangle1 = {
        .x1 = 100,
        .y1 = 200,
        .x2 = 200,
        .y2 = 100,
        .x3 = 300,
        .y3 = 300,
        .fillcolor = 0xffff0000,
        .scale = 1.0
    };
    
    triangle_t exampletriangle2 = {
        .x1 = 50,
        .y1 = 150,
        .x2 = 150,
        .y2 = 50,
        .x3 = 250,
        .y3 = 250,
        .fillcolor = 0xffffff00,
        .scale = 1.0
    };
    
    triangle_t exampletriangle3 = {
        .x1 = 350,
        .y1 = 350,
        .x2 = 460,
        .y2 = 300,
        .x3 = 500,
        .y3 = 400,
        .fillcolor = 0xff00ff00,
        .scale = 1.0
    };
    
    triangle_t exampletriangle4 = {
        .x1 = 350,
        .y1 = 100,
        .x2 = 450,
        .y2 = 50,
        .x3 = 500,
        .y3 = 200,
        .fillcolor = 0xff0000ff,
        .scale = 1.0
    };
    
    // First function run in your program
    int main(int argc, char **argv)
    {
        int retval, done;
        SDL_Surface *screen;
        SDL_Event event;
        
        // Initialize SDL   
        retval = SDL_Init(SDL_INIT_VIDEO);
        if (retval == -1) {
            printf("Unable to initialize SDL\n");
            exit(1);    
        }
        
        //Create a 1024x768x32 window
        screen = SDL_SetVideoMode(1024, 768, 32, 0);     
        if (screen == NULL) {
            printf("Unable to get video surface: %s\n", SDL_GetError());    
            exit(1);
        }
    
        // The teapot is represented as an array of triangle data structures.
        // To draw it on the screen you need to traverse the 'teapot_model' array
        // and call DrawTriangle for each triangle (teapot_data.h contains the array).  
        // The definition TEAPOT_NUMTRIANGLES specifies the number of triangles in the array.
        // The teapot model is contained within a 1000x1000 box (coordinates
        // from -500 to 500 on the x and y axis).  Remember to translate the
        // model to the middle of the screen before drawing it (initialize 
        // triangle->tx and triangle->ty with the appropriate coordinates).
          
        // Draw some example triangles on the screen. 
        // Use these examples in the beginning.
        DrawTriangle(screen, &exampletriangle1);
        DrawTriangle(screen, &exampletriangle2);
        DrawTriangle(screen, &exampletriangle3);
        DrawTriangle(screen, &exampletriangle4);
    
        // Wait for ctrl-c from user
        done = 0;
        while (done == 0) {
            while (SDL_PollEvent(&event)) {
                switch (event.type) {
                case SDL_QUIT:
                    done = 1;
                    break;  
                }           
            }
        }   
        
        SDL_Quit();
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct X & C# - Teapot
    By AlexIllsley in forum C# Programming
    Replies: 3
    Last Post: 08-14-2009, 03:52 AM
  2. Drawing triangles in C#
    By John_L in forum C# Programming
    Replies: 2
    Last Post: 03-15-2008, 08:48 AM
  3. crazy triangles
    By markg in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 12:50 PM
  4. ascending triangles
    By bluegoo06 in forum C++ Programming
    Replies: 9
    Last Post: 09-29-2004, 09:30 AM
  5. clipping triangles.
    By thesanman112 in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 07:22 PM