I need help on making a sine wave I'm trying to do the equation y = Acos(2 (pi)/B x - 2(pi)/B P) amplitude A, period B, and phase shift P. It is not working out too well so can anyone help me refine this?

#include <math.h>
#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;


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
    double amplitude;
    double period;
    double phaseAngle;
    const double PI = 3.1415927;        // constant Pi
    RECT rect;                          // rectangle for the output window
    int xMin, xMax, wdRect;             // min & max x rectangle coords
    int y, x;

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

    // get the rectangle info for this window
    GetClientRect(HOutput, &rect);
    wdRect = rect.right - rect.left;
    xMin = -wdRect / 2;
    xMax =  wdRect / 2;

    // get the user input from the edit boxes and 
    // convert string input to double
    GetDlgItemText(HDialog, IDC_EDIT_AMPLITUDE, str, 32);
    amplitude = atof(str);
    GetDlgItemText(HDialog, IDC_EDIT_PERIOD, str, 32);
    period = atof(str);
    GetDlgItemText(HDialog, IDC_EDIT_PHASEANGLE, str, 32);
    phaseAngle = atof(str);

    // Add the for loop here, that creates the points (x,y)
    //one pixel at a time and then adds the points via the
    // command PGraphics->AddPoint(x,y,green)
    y = amplitude cos(2(3.14)/period) x - (2(3.14)/period) phaseAngle);



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

Recommended Answers

All 3 Replies

Did you miss reading all of the information posted all over this site about CODE tags, like
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) and even on the background of the box you actually typed you message in
????

> y = amplitude cos(2(3.14)/period) x - (2(3.14)/period) phaseAngle);
Whereas multiply is implied in mathematical expressions, you need to be explicit in your code.
Like amplitude * cos .....

Also, why use 3.14 when you already defined PI ?

> Also, why use 3.14 when you already defined PI ?
I get the feeling that he didn't write this code, and didn't take the time to look through it first to see what resources have already been given. If he did write the code, then im amazed that he is doing some quite complex windows stuff without knowing the very basics.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.