Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 107,710 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,109 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Help with line drawing program

 
Reply to this topicStart new topic

Help with line drawing program

spooky3333
post 20 Jul, 2008 - 01:56 PM
Post #1


New D.I.C Head

*
Joined: 11 May, 2008
Posts: 10

Ineed help making this program draw a parabola that is just a line of the parabola...I got as far as making the parabola but its shaded in I just want the parabola lines not shaded in...heres the code.

CODE


/*
parabola2.cpp

Draws a parabola with horizontal axis (pg 52)
made up of line segments
*/

#include <stdio.h>
#include <windows.h>

#include "resource.h"

// your path for this include may vary
#include "GraphicsFramework.h"

// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;

HWND HOutput = 0;  // handle to the output control
HWND HDialog = 0;

// function to get the absolute value of an integer
int Abs(int x) {
    if (x < 0)  return -x;
    else        return x;
}

// function to get the sign (+1 or -1) of an integer
int Sign(int x) {
    if (x < 0)  return -1;
    else        return 1;
}

void drawLine(int xPrev, int yPrev, int x, int y)
{
    int x1 = xPrev;
    int y1 = yPrev;
    int x2 = x;
    int y2 = y;
    int dy = y2 - y1;
    int dx = x2 - x1;
    COLORREF purple = RGB(255, 0, 255);

    

    if (Abs(dy) > Abs(dx)) {
        // since there is a greater change in y than x we must
        // loop in y, calculate x and draw
        for (y=y1; y != y2; y += Sign(dy)) {
            x = x1 + (y - y1) * dx / dy;
            PGraphics->AddPoint(x, y, purple);
        }
    }else {
        // since there is a greater (or equal) change in x than y we must
        // loop in x, calculate y and draw
        for (x=x1; x != x2; x += Sign(dx)) {
            y = y1 + (x - x1) * dy / dx;
            PGraphics->AddPoint(x, y, purple);
    
        }
    }

    // draw the last pixel
    PGraphics->AddPoint(x2, y2, purple);

    // draw the points
    PGraphics->Draw();
}

void DrawStuff() {
    COLORREF green = RGB(0, 255, 0);     // green color to draw with
    COLORREF purple = RGB(255, 0, 255);  // purple color to draw with

    char str[32];                       // string to store user input
    int h, k;                           // parabola vertex
    double a;                           // parabola constant - might be a decimal
    int x, y;                           // loop and point variables
    int xPrev, yPrev;                   // previous point for drawng line segments
    int ymin, ymax;                     // limits for y loop
    RECT rect;                          // rectangle for the output window

    // get the user input from the edit boxes and
    // convert string input to integer
    GetDlgItemText(HDialog, IDC_EDIT_VERTEXX, str, 32);
    h = atoi(str);
    GetDlgItemText(HDialog, IDC_EDIT_VERTEXY, str, 32);
    k = atoi(str);
    GetDlgItemText(HDialog, IDC_EDIT_CONSTA, str, 32);
    a = atof(str);                              // use atof to allow user to enter a decimal

    // get the rect for this window
    GetClientRect(HOutput, &rect);

    // use the rectangle info to set up y loop limits
    ymin = -(rect.bottom - rect.top) / 2;
    ymax =  (rect.bottom - rect.top) / 2;

    // clear the scene and add an axis
    PGraphics->ClearScene(RGB(0, 0, 0));
    PGraphics->AddAxis(RGB(150, 150, 150), 10);

    // loop in y, calculate x and draw
    yPrev = ymin;
    xPrev = (int) (a * (yPrev-k)*(yPrev-k))+h;
    for (y = ymin; y <= ymax; y++) {
        x = (int)( a * (y-k) * (y-k) ) + h;
        PGraphics->AddPoint(x, y, green);
        drawLine(xPrev, yPrev, x, y);
        
    }

    // draw the points
    PGraphics->Draw();
}

/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:
        // dialog is initializing - store the picture box handle in a global variable for later
        HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);        

        // instantiate and initialize our graphics framework object
        PGraphics = new GraphicsFramework(HOutput);

        break;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDC_BTN_DRAW:
                // draw button was pressed
                DrawStuff();
                break;
            case IDC_BTN_CLEAR:
                // clear button was pressed so clear the scene and draw the empty scene
                PGraphics->ClearScene(RGB(0, 0, 0));
                PGraphics->Draw();
                break;
            case IDCANCEL:
                // user is quitting so release the GraphicsFramework object and quit
                delete PGraphics;
                PostQuitMessage(0);
                break;
        }
                  
    }
    return FALSE;
}

// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    // create the main window
    // store its handle in a global if needed
    HDialog = CreateDialog (GetModuleHandle(NULL),
        MAKEINTRESOURCE(IDD_DIALOG1),
        0,
        DialogProc);

    // make the dialog visible
    ShowWindow(HDialog, SW_SHOW);

    // standard windows message loop
    MSG  msg;
    int status;
    while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        // avoid processing messages for the dialog
        if (!IsDialogMessage (HDialog, & msg))
        {
            TranslateMessage ( & msg );
            DispatchMessage ( & msg );
        }
    }

    return (int)(msg.wParam);
}

User is offlineProfile CardPM

Go to the top of the page


perfectly.insane
post 21 Jul, 2008 - 07:22 PM
Post #2


D.I.C Regular

Group Icon
Joined: 22 Mar, 2008
Posts: 462



Thanked 33 times
My Contributions


cpp

// loop in y, calculate x and draw
yPrev = ymin;
xPrev = (int) (a * (yPrev-k)*(yPrev-k))+h;
for (y = ymin; y <= ymax; y++) {
x = (int)( a * (y-k) * (y-k) ) + h;
PGraphics->AddPoint(x, y, green);
drawLine(xPrev, yPrev, x, y);

}


My best guess is that it's related to the drawLine call above... Since xPrev and yPrev are not being updated in the loop, the line is always being drawn directly from the initial point. Make sure that xPrev and yPrev are updated at the end of the loop, and I think it should then work better.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/30/08 03:14AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month