cosine graph

I am still new at programming and I am working on a assignment to draw a cosine graph. I have most of the code written but I think I am getting a infinite loop. I don't want you all to do it but any information you could provide would help.
1
2
3
4
5
6
7
for (int x = xMin; x <= xMax; x++) {

	 y = cos((double)2*PI/180);
	 x = amplitude * cos((2 * PI * y) / period + phaseAngle);

     PGraphics->AddPoint(x,y,green);
}

Unless xMin is more than xMax, which i find unlikely, I don't think you'll be in an infinite loop, please post more of your code.
What is data type of x, xMin and xMax, and what are max value they can take. Note all three should be same type
Here is all of the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
 /*
cosine_soln.cpp

Draws a cosine function using line segments
*/

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

#include "resource.h"
#pragma intrinsic (sin, cos, tan)
// your path for this include may vary
#include "GraphicsFramework.h"
int x;
double y;
double rad;
// 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
	double rad = PI/180;
    // 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)



for (int x = xMin; x <= xMax; x++) {

	 y = cos((double)2*PI/180);
	 

     PGraphics->AddPoint(x,y,green);
}

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

Ok I took out this line
y = amplitude * cos((2 * PI * x) / period + phaseAngle);
and changed the x in this to y in this one.
x = cos((double)2*PI/180);
and it draws a line all the way on the x axis. Now I just need to figure out how to get the waves in it.
I don't think that they are the same and when I try to change x it freezes up. Same thing when I change the y.
I read something on the assignment about radius. I am working on how to include that in the program but havent figured that out just yet. I have a picture of what it needs to look like but havent figured out how to post it.
Last edited on
You are modifying x inside the for loop, which is utterly bad and can cause an infinite loop.
I was thinking it was an infinite loop. But I need to make a wave as the output. Is it possible to do that without modifing x in the loop? I will keep trying withoug modifying x and see what I can come up with.
I don't understand the reasoning behind your equations. Y is constant, and if phaseAngle and amplitude are constant, so will X. So you are just adding the same point over and over and over.
Why not:

1
2
3
4
5
for(int x = 0; x < 90; x++)
{
    y = cos(x * PI / 180);
    PGraphics->AddPoint(x, y, color);
}

??

If you want it with amplitude, just multiply by the amplitude.
Last edited on
From what I was reading it says to use the equation y =amplitude * cos( 2*PI/period x -2*PI/period phaseAngle). I got it to move a little by just adding 2 * cos at the beginning of the y equation. I will work on multipling the amplitude and see if that is what I need to do.
I have got it todraw the graph but it is upside down.
1
2
3
4
5
6
7
for (int x = xMin; x <= period; x++) {

	 y = 2 * cos((double)x*PI/180);
	y =  y * amplitude ;

     PGraphics->AddPoint(x,y,green);
}
Multiplying by -1 the y will flip it around the X axis.
I got it now. Thank you all for your help.
Topic archived. No new replies allowed.