Thread: Making a teapot using triangles

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    1

    Making a teapot using triangles

    This is the assignment:


    Your task is to complete the functions listed below. Together these functions provide functionality to draw filled triangles on the screen. In addition, the functions enable you to scale the size of a triangle and move (translate) a triangle to a given position on the screen.
    Each triangle is described by a triangle_t data structure. All the functions below accept a pointer to a triangle data structure as input. Each function needs to access and modify the fields of the data structure as appropriate.

    • ScaleTriangle - Scale the size of the triangle. The scaling factor is specified in the scale field in the triangle data structure. The scaling factor is a floating point number. A factor less than 1.0 should decrease the size of the triangle, while a factor greater than 1.0 should increase the size.
    • TranslateTriangle - Move the triangle to a specific position on the screen. The position is specified in the triangle data structure in the tx and ty fields.
    • CalculateTriangleBoundingBox - Calculate the size of a rectangle that is just large enough to contain the triangle. The bx, by, bw, and bh fields of the triangle data structure should be initialized with the appropriate values.
    • FillTriangle - Fill the triangle with a color. The fillcolor field in the triangle data structure specifies the fill color. There exists many different approaches for filling triangles and polygons in general. See the Resources section below for some inspiration.
    • DrawTriangle - Draw a scaled, translated, and filled triangle on the screen.
    I have managed to draw the teapot and translated it onto the screen, and I have tried to make the bounding box and fill the teapot with color. But I have done something wrong, and I do not understand what it is, so I would be grateful if someone could help me.

    PS: I am new to programming, and I have really tried my best, and worked on this for hours.

    Here is the code so far: (the parts I have problems with are in bold)
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include "SDL.h"
    #include "triangle.h"
    #include "drawline.h"
    #include <math.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)
    {
        float dx1=0, dx2=0, dx3=0;
        float dy1=0, dy2=0, dy3=0;
        
        // 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->y2;
        triangle->sy2 = triangle->y1;
        triangle->sy3 = triangle->y3;
        
        dx1=((triangle->x2-triangle->x1)*triangle->scale);
        dx2=((triangle->x3-triangle->x2)*triangle->scale);
        dx3= ((triangle->x1-triangle->x3)*triangle->scale);
        dy1=((triangle->y2-triangle->y1)*triangle->scale);
        dy2= ((triangle->y3-triangle->y2)*triangle->scale);
        dy3= ((triangle->y1-triangle->y3)*triangle->scale);
        
        triangle->sx1= triangle->sx3+dx3;
        triangle->sx2= triangle->sx1+dx1;
        triangle->sx3=triangle->sx2+dx2;
        triangle->sy1= triangle->sy3+dy3;
        triangle->sy2= triangle->sy1+dy1;
        triangle->sy3=triangle->sy2+dy2;
    }
    
    
    
    
    // Move triangle to its screen position
    void TranslateTriangle(triangle_t *triangle)
    {
        triangle->tx = 1024/2;
        triangle->ty = 768/2;
    
    
        triangle->sx1=(triangle->tx+triangle->sx1);
        triangle->sx2 = (triangle->tx+triangle->sx2);
        triangle->sx3 =(triangle->tx+triangle->sx3);
        triangle->sy1 = (triangle->ty+triangle->sy1);
        triangle->sy2= (triangle->ty+triangle->sy2);
        triangle->sy3 = (triangle->ty+triangle->sy3);
        
        // 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)
    {
        // Calculate max value for x
        triangle->bw = triangle->sx1;
        if (triangle->sx2 > triangle->bw)
        {
            triangle->bw = triangle->sx2;
        }
        if (triangle->sx3 > triangle->bw)
        {
            triangle->bw = triangle->sx3;
        }
    
    
        // Calculate min value for x
        triangle->bx = triangle->sx1;
        if (triangle->sx2 < triangle->bx)
        {
            triangle->bx = triangle->sx2;
        }
        if (triangle->sx3 < triangle->bx)
        {
            triangle->bx = triangle->sx3;
        }
        
        // Calculate max value for y
        triangle->by = triangle->sy1;
        if (triangle->sy2 > triangle->by)
        {
            triangle->by = triangle->sy2;
        }
        if (triangle->sy3 > triangle->by)
        {
            triangle->by = triangle->sy3;
        }
        
        // Calculate min value for y
        triangle->bh = triangle->sy1;
        if (triangle->sy2 < triangle->bh)
        {
            triangle->bh = triangle->sy2;
        }
        if (triangle->sy3 < triangle->bh)
        {
            triangle->bh = triangle->sy3;
        }
        triangle->bw = triangle->bw - triangle->bx;
        triangle->by = triangle->by - triangle->bh; 
        
    
    
        // 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)
    {
        
        int i, j, k;
    
    
        for (k = triangle->bw; k <= triangle->bh; k++)
        {
        for(i = triangle->bx; i <= triangle->bw; i++)
        
            if (GetPixel(screen, i, k) == TRIANGLE_PENCOLOR)
            {
                break;
            }
        for (j = triangle->by; j <= triangle->bh; j++)
            if (GetPixel(screen, j, k) == TRIANGLE_PENCOLOR)
            {
                break;
            }
    
    
        
            DrawLine(screen, i, k, j, k, triangle->fillcolor);
        }
    
    
    }
        // 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.)
        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->sx3, triangle->sy3, triangle->sx1, triangle->sy1, 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);
        SDL_UpdateRect(screen, triangle->bx, triangle->by, triangle->bw, triangle->bh);
    }
    Attached Images Attached Images Making a teapot using triangles-teapot-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,312
    There seems to be a mis-understanding as to what your bounding box actually is.

    When you calculate the bounding box, it's just the width and height.
    Code:
        triangle->bw = triangle->bw - triangle->bx;
        triangle->by = triangle->by - triangle->bh; 
        
    
    
        // 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)
    {
        
        int i, j, k;
    
    
        for (k = triangle->bw; k <= triangle->bh; k++)
        {
        for(i = triangle->bx; i <= triangle->bw; i++)
    Yet here, it looks like you're treating them as coordinates.

    Instead of a teapot, create a single triangle and single-step through the code with a debugger.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a teapot in C with triangles
    By RST in forum C Programming
    Replies: 5
    Last Post: 10-15-2013, 03:17 PM
  2. Making a teapot using triangles.
    By LEL in forum C Programming
    Replies: 21
    Last Post: 10-15-2013, 11:03 AM
  3. Direct X & C# - Teapot
    By AlexIllsley in forum C# Programming
    Replies: 3
    Last Post: 08-14-2009, 03:52 AM
  4. Identifying Triangles
    By howeezy in forum C++ Programming
    Replies: 5
    Last Post: 10-08-2005, 12:14 PM
  5. Equilateral Right Triangles
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-06-2002, 03:32 PM