#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0

    Drawing, scaling, moving and filling triangles.


    Hi.

    First of all, I'm completely new to c programming. I am familiar with simple if, while and for statements and such, but on this subject that I'm about to present for you, I'm tempted to say that I have no clue about what I should be doing. Or maybe a more correct way to say it would be that I know what to do, I just don't know how or where to do it.

    I've received an assignment that reads as follows:

    Problem set 4 - Mandatory

    Overview

    In this assignment you will implement C code for drawing filled triangles in a window on the screen.

    Description

    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.

    In this assignment there should be no need to define functions beyond those already defined in the files we provide. We recommend starting with code to draw a wireframe version of the triangle on the screen (by use of the DrawLine function). Then, either proceed with the code to scale the triangle, or the code to move the triangle to a given position on the screen. Note that triangle scale is specified as a floating point number, while the triangle coordinates are integer variables. You will need to think about conversions between integer and floating point numbers to make the scaling function work properly. When scaling, translation, and drawing works, embark on the code for filling the triangles.

    The teapot model is represented as an array of pre-initialized triangle data structures. The model is contained within a 1000x1000 box, with coordinates ranging from -500 to 500 on both the x and y axis. To properly draw this model you must translate it to the middle of the screen. Note that in this assignment we set the resolution of the screen to 1024x768 pixels. If translated to the middle of the screen, the teapot model does not have y coordinates that exceed the resolution of the screen. Thus there is no need to scale the teapot before drawing it.

    Code

    Your starting point is the following set of files:

    drawline.h - Specifies the interface of the drawline function. Do not modify this file.

    drawline.c - Implements the DrawLine function.

    triangle.h - Specifies the triangle data structure and the interface to the DrawTriangle function. Do not modify this file.

    triangle.c - Empty stubs for DrawTriangle and other functions.

    teapot_data.h - Coordinates for the classic teapot model.

    main.c - Contains the main function, calls to initialize SDL, and calls to draw some example triangles.

    Makefile - A Makefile for compiling the code.

    We've bundled all of these files in a zip file. The zip file is located in the same folder as this document. Use this zip file as a starting point, as it also includes the necessary SDL files.

    If needed, I can try to figure out a way to upload the zip file, or in some other way supply you with the needed code to help me out here.

    I do not by any means want or need someone to do this assignment for me. However, I'm very new to this, and I will need a lot of help. I will try to do as much as possible on my own because I feel that's the best way to learn, and then ask questions whenever I'm stuck.

    I know I haven't provided any of the code yet, and I'm not sure how much you can take out from the assignment text I have provided, but I'll try to explain what I've done so far.

    I've made 4 triangles with this code:

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

    Next up I'm looking to write a code that can scale these triangles, where >1 makes them larger, and <1 makes them smaller.

    After that, I'm looking to move the triangles around to a said coordinate.

    Next I want to fill the triangles with a color.

    Finally I can draw the teapot.

    Please let me know if there's anything I can edit to make this post easier to read and understand, and if there's anything I can add to give you a better understanding of my assignment. I have a zip file with a lot of files I want to upload if you need it, or otherwise can supply you with the code that follows with this assignment.

    This is as follows:

    Pre Code

    drawline.h -Specifies the interface of the drawline function. Do not modify this file.

    drawline.c - Implements the DrawLine function.

    triangle.h - Specifies the triangle data structure and the interface to the DrawTriangle function. Do not modify this file.

    triangle.c - Empty stubs for DrawTriangle and other functions. INSERT YOUR CODE
    HERE.

    teapot_data.h - Coordinates for the classic teapot model.

    main.c - Contains the main function, calls to initialize SDL, and calls to draw some
    example triangles. INSERT YOUR CODE HERE

    Makefile - A Makefile for compiling the code.
    SDL32/64.dll and include/ - SDL libraries for Windows
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    I know this is a school assignment and it might not be the most interesting thing in the world, in addition to the fact that I have barely started. Believe me though, I'm working hard towards understanding this as it seems very interesting to know the mechanics behind how computers work etc. Programming is used to a lot, and that's why I'm interested to learn about it beyond the fact that I have to learn it in some way to get a degree in my subject.

    Therefore, any input is appreciated. If only to tell me how far I should get before I can even think about asking for directions, guidelines or any help what so ever on here, or any missing info from my zip file. Or maybe you have a no home work policy.

    I really don't know. Just any feedback at all would be nice.
  4. #3
  5. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    Study how to make lines and circles. Filling shapes is merely a matter of changing pixels within a boundary.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by DaWei_M
    Study how to make lines and circles. Filling shapes is merely a matter of changing pixels within a boundary.
    Thanks a lot for your reply.

    I will do this, obviously, but I'm kind of lost. But I will of course look for tips and guidelines on how to do this, I was just hoping someone could help me explain how I should be able to see what I should be coding if you know what I mean.

    I 'fear' that if I find some useful code I will just implement it into the program and hope it works without understanding it.

    I do however know how to make lines, but that doesn't mean I understand it.

    This is such a huge assignment for my level of programming, but I'll do my best.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    This is such a huge assignment for my level of programming, but I'll do my best.
    That's a really odd assignment for a novice. Unless you're a undergraduate student at some game design school such as Full Sail. My curiosity is piqued. Are you such a student?

    But anyway, please post all your source code. It would allow us to "tinker" with it in order to provide suggestions etc.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by BobS0327
    That's a really odd assignment for a novice. Unless you're a undergraduate student at some game design school such as Full Sail. My curiosity is piqued. Are you such a student?

    But anyway, please post all your source code. It would allow us to "tinker" with it in order to provide suggestions etc.
    I'm not. We have this subject as an introduction to programming and how the computer works. We've had it for less than two months.


    I'll try to post my 'source code' and please let me know if I provide the wrong information.

    Main:

    PHP 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 argcchar **argv)
    {
        
    int retvaldone;
        
    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(1024768320);     
        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;


    Triangle.c: (my 3 lines of code is shown with 'by OP' next to them)

    PHP 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 *trianglechar *msg)
    {
        
    printf("%s: %d,%d - %d,%d - %d,%d\n",
            
    msg,
            
    triangle->x1triangle->y1
            
    triangle->x2triangle->y2
            
    triangle->x3triangle->y3);
            
        
    }


    // Return 0 if triangle coordinates are outside the screen boundary. 1 otherwise.
    int SanityCheckTriangle(SDL_Surface *screentriangle_t *triangle)
    {
        if (
    triangle->sx1 || triangle->sx1 >= screen->||
            
    triangle->sx2 || triangle->sx2 >= screen->||
            
    triangle->sx3 || triangle->sx3 >= screen->||
            
    triangle->sy1 || triangle->sy1 >= screen->||
            
    triangle->sy2 || triangle->sy2 >= screen->||
            
    triangle->sy3 || 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 *screentriangle_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 *screentriangle_t *triangle)
    {
        
    int isOK;
        
        
    // Scale.
        
    ScaleTriangle(triangle);
        
        
    void ScaleTriangle(triangle_t *triangle)
        {

        }
        
        
        
    // Translate.
        
    TranslateTriangle(triangle);
        
        
    // Determine bounding box
        
    CalculateTriangleBoundingBox(triangle);

        
    // Sanity check that triangle is within screen boundaries.
        
    isOK SanityCheckTriangle(screentriangle);
        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 code
        
        
    DrawLine(screentriangle->sx1triangle->sy1triangle->sx2triangle->sy2TRIANGLE_PENCOLOR);            //code by OP to draw a line for the triangle
        
    DrawLine(screentriangle->sx2triangle->sy2triangle->sx3triangle->sy3TRIANGLE_PENCOLOR);            //code by OP to draw a line for the triangle
        
    DrawLine(screentriangle->sx1triangle->sy1triangle->sx3triangle->sy3TRIANGLE_PENCOLOR);            //code by OP to draw a line for the triangle

        // Fill triangle
        
    FillTriangle(screentriangle);

        
    // 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(screen00screen->wscreen->h);


    Drawline.c:

    PHP Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <assert.h>
    #include "SDL.h"
    #include "drawline.h"

    // Read pixel x,y on the screen
    unsigned int GetPixel(SDL_Surface *screenint xint y)
    {
        
    unsigned int *bufp;

        if (
    >= screen->||  ||
            
    >=screen->|| 0) {
             
    printf("Accessing pixel outside of screen, check translation or scale\n");
             return 
    0;
        }

        
    // Set pixel
        
    bufp = (unsigned int*)screen->pixels y*screen->pitch/x;
        return *
    bufp;
    }

    // Set pixel x,y on the screen
    void SetPixel(SDL_Surface *screenint xint yunsigned int color)
    {
        
    unsigned int *bufp;

        if (
    >= screen->||  ||
            
    >=screen->|| 0) {
             
    printf("Plotting pixel outside of screen, check translation or scale\n");
             return;
        }

        
    // Set pixel
        
    bufp = (unsigned int*)screen->pixels y*screen->pitch/x;
        *
    bufp color;     
    }

    // Draw a line on the screen from x1,y1 to x2,y2
    void DrawLine(SDL_Surface *screenint x1int y1int x2int y2unsigned int color)
    {
        
    int fraction;
        
    int xdxstepx;
        
    int ydystepy;
        
        
        
    // The below code implements the classic Bresenham algorithm
        
        
    dx x2 x1;
        
    dy  y2 y1;
            
        if (
    dy 0) {
            
    dy = -dy;
            
    stepy = -1;
        } else {
            
    stepy 1;    
        }
        
        if (
    dx 0) {
            
    dx = -dx;
            
    stepx = -1;    
        } else {
            
    stepx 1;
        }
        
        
    dy dy*2;
        
    dx dx*2;
        
    x1;
        
    y1;
        
    SetPixel(screenxycolor);
        if (
    dx dy) {
            
    fraction dy - (dx/2);
            while (
    != x2) {
                if (
    fraction >= 0) {
                    
    stepy;
                    
    fraction fraction dx;    
                }
                
    stepx;
                
    fraction fraction dy;
                
    SetPixel(screenxycolor);
            }    
        } else {
            
    fraction dx - (dy/2);
            while (
    != y2) {
                if (
    fraction >= 0) {
                    
    stepx;
                    
    fraction fraction dy;    
                }
                
    stepy;
                
    fraction fraction dx;
                
    SetPixel(screenxycolor);
            }    
        }


    These are the 3 .c files the follows with the assignment. I also have a drawline.h file, triangle.h and teapot_data.h.

    Let me know if you need more info or if I've posted something wrong.

    Thanks for showing interest in my problem!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    This is what I've done so far on the scaling.

    Tried to find the centroid, and then attempt to do the scaling part.

    It did not work, however. Why am I not getting any response?

    PHP 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 *trianglechar *msg)
    {
        
    printf("%s: %d,%d - %d,%d - %d,%d\n",
            
    msg,
            
    triangle->x1triangle->y1
            
    triangle->x2triangle->y2
            
    triangle->x3triangle->y3);
            
        
    }


    // Return 0 if triangle coordinates are outside the screen boundary. 1 otherwise.
    int SanityCheckTriangle(SDL_Surface *screentriangle_t *triangle)
    {
        if (
    triangle->sx1 || triangle->sx1 >= screen->||
            
    triangle->sx2 || triangle->sx2 >= screen->||
            
    triangle->sx3 || triangle->sx3 >= screen->||
            
    triangle->sy1 || triangle->sy1 >= screen->||
            
    triangle->sy2 || triangle->sy2 >= screen->||
            
    triangle->sy3 || 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 *screentriangle_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 *screentriangle_t *triangle)
    {
        
    int isOK;
        
        
    // Scale.
        
    ScaleTriangle(triangle);
        
        
    void ScaleTriangle(triangle_t *triangle)
        {
            
    int x1x2x3;
            
    int y1y2y3;
            
    int nx1nx2nx3;
            
    int ny1ny2ny3;
            
    int cxcy
            float scale
    ;
            
            
    x1 triangle->x1;
            
    x2 triangle->x2;
            
    x3 triangle->x3;
            
    y1 triangle->y1;
            
    y2 triangle->y2;
            
    y3 triangle->y3;
            
            
    scale triangle->scale;
            
            
    cx = (x1 x2 x3)/3;
            
    cy = (y1 y2 y3)/3;
            
            
    triangle->bx cx;
            
    triangle->by cy;
            
        
            
            
            
            
            
        }
        
        
        
    // Translate.
        
    TranslateTriangle(triangle);
        
        
    // Determine bounding box
        
    CalculateTriangleBoundingBox(triangle);

        
    // Sanity check that triangle is within screen boundaries.
        
    isOK SanityCheckTriangle(screentriangle);
        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 code
        
        
    DrawLine(screentriangle->sx1triangle->sy1triangle->sx2triangle->sy2TRIANGLE_PENCOLOR);            //code to draw a line for the triangle
        
    DrawLine(screentriangle->sx2triangle->sy2triangle->sx3triangle->sy3TRIANGLE_PENCOLOR);            //code to draw a line for the triangle
        
    DrawLine(screentriangle->sx1triangle->sy1triangle->sx3triangle->sy3TRIANGLE_PENCOLOR);            //code to draw a line for the triangle
        
    SetPixel(screentriangle->bxtriangle->by0xffffff);
        
        
    // Fill triangle
        
    FillTriangle(screentriangle);

        
    // 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(screen00screen->wscreen->h);

  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    As it turns out, I was trying to write the code in the wrong place. I've fixed that now, and I've moved on to trying to move the triangle.

    This is my current progress, with the whole code posted below.

    Scaling (done):

    PHP Code:
    // 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.

        
        
            
    int x1x2x3;
            
    int y1y2y3;
            
    int nx1nx2nx3;
            
    int ny1ny2ny3;
            
    int cxcy;
            
    float scale;
            
            
    x1 triangle->x1;
            
    x2 triangle->x2;
            
    x3 triangle->x3;
            
    y1 triangle->y1;
            
    y2 triangle->y2;
            
    y3 triangle->y3;
            
            
    scale triangle->scale;
            
            
    cx = (x1 x2 x3)/3;
            
    cy = (y1 y2 y3)/3;
            
            
    triangle->bx cx;
            
    triangle->by cy;
            
            
    printf("%d, %d\n"cxcy);
            
            
    nx1 cx + (x1 cx)*1.2;
            
    nx2 cx + (x2 cx)*1.2;
            
    nx3 cx + (x3 cx)*1.2;
            
    ny1 cy + (y1 cy)*1.2;
            
    ny2 cy + (y2 cy)*1.2;
            
    ny3 cy + (y3 cy)*1.2;
        
        
        
        
    triangle->sx1 nx1;
        
    triangle->sx2 nx2;
        
    triangle->sx3 nx3;
        
    triangle->sy1 ny1;
        
    triangle->sy2 ny2;
        
    triangle->sy3 ny3;

    Moving triangle:

    I'm stuck with this one for a while now. I'm not too sure where to move on from now with pointers and such. Any help here would be greatly appreciated.

    PHP Code:
    // 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.)
        
        
    int txty;
        
    int fx1fx2fx3fy1fy2fy3;
        
        
    fx1 triangle->sx1
        fx2 
    triangle->sx2
        fx3 
    triangle->sx3
        fy1 
    triangle->sy1
        fy2 
    triangle->sy2
        fy3 
    triangle->sy3
        
        
        
        
        
        
        

    PHP 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 *trianglechar *msg)
    {
        
    printf("%s: %d,%d - %d,%d - %d,%d\n",
            
    msg,
            
    triangle->x1triangle->y1
            
    triangle->x2triangle->y2
            
    triangle->x3triangle->y3);
            
        
    }


    // Return 0 if triangle coordinates are outside the screen boundary. 1 otherwise.
    int SanityCheckTriangle(SDL_Surface *screentriangle_t *triangle)
    {
        if (
    triangle->sx1 || triangle->sx1 >= screen->||
            
    triangle->sx2 || triangle->sx2 >= screen->||
            
    triangle->sx3 || triangle->sx3 >= screen->||
            
    triangle->sy1 || triangle->sy1 >= screen->||
            
    triangle->sy2 || triangle->sy2 >= screen->||
            
    triangle->sy3 || 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.

        
        
            
    int x1x2x3;
            
    int y1y2y3;
            
    int nx1nx2nx3;
            
    int ny1ny2ny3;
            
    int cxcy;
            
    float scale;
            
            
    x1 triangle->x1;
            
    x2 triangle->x2;
            
    x3 triangle->x3;
            
    y1 triangle->y1;
            
    y2 triangle->y2;
            
    y3 triangle->y3;
            
            
    scale triangle->scale;
            
            
    cx = (x1 x2 x3)/3;
            
    cy = (y1 y2 y3)/3;
            
            
    triangle->bx cx;
            
    triangle->by cy;
            
            
    printf("%d, %d\n"cxcy);
            
            
    nx1 cx + (x1 cx)*1.2;
            
    nx2 cx + (x2 cx)*1.2;
            
    nx3 cx + (x3 cx)*1.2;
            
    ny1 cy + (y1 cy)*1.2;
            
    ny2 cy + (y2 cy)*1.2;
            
    ny3 cy + (y3 cy)*1.2;
        
        
        
        
    triangle->sx1 nx1;
        
    triangle->sx2 nx2;
        
    triangle->sx3 nx3;
        
    triangle->sy1 ny1;
        
    triangle->sy2 ny2;
        
    triangle->sy3 ny3;
    }


    // 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.)
        
        
    int txty;
        
    int fx1fx2fx3fy1fy2fy3;
        
        
    fx1 triangle->sx1
        fx2 
    triangle->sx2
        fx3 
    triangle->sx3
        fy1 
    triangle->sy1
        fy2 
    triangle->sy2
        fy3 
    triangle->sy3
        
        
        
        
        
        
        
    }


    // 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 *screentriangle_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 *screentriangle_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(screentriangle);
        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 code
        
        
    DrawLine(screentriangle->sx1triangle->sy1triangle->sx2triangle->sy2TRIANGLE_PENCOLOR);            //code to draw a line for the triangle
        
    DrawLine(screentriangle->sx2triangle->sy2triangle->sx3triangle->sy3TRIANGLE_PENCOLOR);            //code to draw a line for the triangle
        
    DrawLine(screentriangle->sx1triangle->sy1triangle->sx3triangle->sy3TRIANGLE_PENCOLOR);            //code to draw a line for the triangle
        
    SetPixel(screentriangle->bxtriangle->by0xffffff);
        
        
    // Fill triangle
        
    FillTriangle(screentriangle);

        
    // 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(screen00screen->wscreen->h);


IMN logo majestic logo threadwatch logo seochat tools logo