Re: CPoint Help

Tech-Archive recommends: Fix windows errors by optimizing your registry



In addition to Scott's reply...

"confusedinc" <confusedinc@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:BD69CA56-4369-4870-87BA-7AEE39B93A7E@xxxxxxxxxxxxxxxx
>I cannot get my CPoint point[8] array to draw lines based on the criteria
> below. I have confused myself to no end. Stuck on this problem. Program
> will
> draw lines without the array, however, I would like to be able to change
> code
> to use the array instead. I'm using VC++ 6.0.
> Thanks!
> Here is what I have so far:
>
> void CProject5View::OnDraw(CDC* pDC)
> {
> double PI = 3.1415926535;
> int x;
> int y = 0;
> int i = 0;
>
> double dStep = (PI * 2) / m_iSamples;
> double dResult = i * dStep;

The multiplication in this initialization is unneeded since you have
initialized i to zero, the result would always be zero.

>
> pDC->MoveTo(60, 200);
> pDC->LineTo(60, 400);
> pDC->LineTo(550, 400);
> pDC->MoveTo(60, 400);
> pDC->LineTo(60, 600);
>
> // I wish to use this array to draw my lines instead of the code that
> follows //
> see sin and cos case statements below
>
>
> CPoint point[8];
> point[0] = (PI/4 * x, 0.707 * y);
> point[1] = (PI/2 * x, 1 * y);
> point[2] = ((3*PI)/4 * x, 0.707 * y);
> point[3] = (PI * x, 0 * y);
> point[4] = ((5*PI)/4 * x, -0.707 * y);
> point[5] = ((3*PI)/2 * x, -1 * y);
> point[6] = ((7*PI)/4 * x, -0.707 * y);
> point[7] = (2*PI * x, 0 * y);
>

x is uninitialized and y is initialized to zero. What are you attempting
here? What are your calculations for? These values will not draw a sine
wave like your for loop does...

>
> int PtX = 60;
> int PtY = 400;
> int xLine = 500 / m_iSamples;
> int yLine = xLine;
>
> switch (m_Graph) {
> case Sin:
>
> pDC->MoveTo(PtX,PtY);
> for(i=0; i<=m_iSamples; i++) {
> dResult = i*dStep;
> dResult = sin(dResult);
> dResult = 200 * dResult;
> y = (int)dResult;
> y = PtY - y;
> x = PtX + i * xLine;
> pDC->LineTo(x,y);
>
> }
> break;
>
> case Cos:
> pDC->MoveTo(PtX, PtY - 200);
> for(i=0; i<=m_iSamples; i++) {
> dResult = i*dStep;
> dResult = cos(dResult);
> dResult = 200 * dResult;
> y = (int)dResult;
> y = PtY - y;
> x = PtX + i * xLine;
> pDC->LineTo(x,y);
>
> }
> break;
>
> }
> }
>

This will do exactly what your for loop does but using a CPoint array...

// Draw the vertical bar line...
pDC->MoveTo( 60, 200 );
pDC->LineTo( 60, 600 );

// Draw the horizontal line bar...
pDC->MoveTo( 60, 400 );
pDC->LineTo( 550, 400 );

const double PI= 3.1415926535;
int x= 0; // No longer needed...
int y= 0; // No longer needed...
int i= 0;

double dStep= (PI * 2) / m_iSamples;
double dResult= i * dStep;

// Point where drawing will commence...
int PtX= 60;
int PtY= (m_Graph == Sin) ? 400 : 200;

int xLine= 500 / m_iSamples;

// Start where the vertical and horizontal lines intersect...
pDC->MoveTo( PtX, PtY );
CPoint pt[9]; // Assumes that m_iSamples == 8
for( i= 0; i <= m_iSamples; i++ )
{
dResult= i * dStep;
dResult= (m_Graph == Sin) ? sin( dResult ) : cos( dResult );
dResult= 200 * dResult;
pt[i].y= (int)dResult;
pt[i].y= PtY - pt[i].y;
pt[i].x= PtX + i * xLine;
// Draw the line segment...
pDC->LineTo( pt[i].x, pt[i].y );
}

--
============
Frank Hickman
Microsoft MVP
NobleSoft, Inc.
============
Replace the _nosp@m_ with @ to reply.


.



Relevant Pages

  • Re: Improving large image drawing
    ... > My input is an int array, that I would like to display as an image. ... > image to draw to the offscreen buffer of a jpanel... ... > // pixels is a w*h array with an rgb value. ...
    (comp.lang.java.gui)
  • Array help
    ... Does anyone know why I can't draw lines of sine and cosine functions based on ... code the array function to draw the lines correctly. ... double dResult = p * dStep; ...
    (microsoft.public.vc.mfc)
  • Re: Array help
    ... > Does anyone know why I can't draw lines of sine and cosine functions based ... > code the array function to draw the lines correctly. ... > dResult = sin; ...
    (microsoft.public.vc.mfc)
  • Custom Draw LIstCtrl and scrolling
    ... I wrote a custom draw CListCtrl that allow to display different row height on windows mobile ... void CListCtrlCommands::OnSize(UINT nType, int cx, int cy) ... CRect rect; ... GetClientRect(&rect); ...
    (microsoft.public.vc.mfc)
  • Re: Graphics help please
    ... I've got something similar, with a 3D turtle. ... You can also draw a line from to, with the ... final int width = 1024; ... static public void main ...
    (comp.lang.java.programmer)