CPoint an Funktion übergeben



  • Ich steh mal wieder im Walde.
    Ich möchte mir eine Funktion schreiben, mit der ich einen Graphen in einem bestimmten Bereich plotten kann.
    Leider weiß ich nicht, wie ich CPoint richtig an die Funktion übergeben soll.

    void CChildView::DrawGraph(CRect r,CPoint* p,int pSize)
    {
    	CDC* pDC;
    	GetClientRect(rClient);
    	pDC = (CDC*) GetDC();
    	CPoint* point = new CPoint[40];
    	int i,j;
    
    	double ymax = 0;
    	double ymin = 0;
    	double hSize = r.right - r.left;
    	double vSize = r.top - r.bottom;
    
    	long* py = &p->y;
    	long* px = &p->x;
    
    	// find abs. Maximum in p
    	for(i=0;i<pSize;i++)
    	{
    		if(p->y >> sizeof(point[0].y)*i > ymax) ymax = p->y >> sizeof(point[0].y)*i;
    		if(p->y >> sizeof(point[0].y)*i < ymin) ymin = p->y >> sizeof(point[0].y)*i ;
    	}
    
    	j=0;
    	for(i=0;i<pSize;i++)
    	{
    		point[j].x=r.left + i * (r.right-r.left) / pSize;
    		point[j].y=r.bottom - (r.bottom-r.top)/2 - (p->y >> sizeof(point[0].y)*i) * (r.bottom-r.top) / 2;
    		j++;
    	}
    
    	pDC->Polyline(point,pSize);
    
    	ReleaseDC(pDC);
    	delete point;
    }
    

  • Mod

    Und was ist nun Dein Problem? Ich verstehe es nicht?

    BTW: Warum allokierst Du point nicht extakt nach pSize?



  • etsmart schrieb:

    Ich steh mal wieder im Walde.
    Ich möchte mir eine Funktion schreiben, mit der ich einen Graphen in einem bestimmten Bereich plotten kann.
    Leider weiß ich nicht, wie ich CPoint richtig an die Funktion übergeben soll.

    Was verstehst Du unter "richtig übergeben"? Wenn der Parameter immer erforderlich ist und in der aufrufenden Funktion nicht verändert werden soll/darf, würde ich CPoint const & nehmen.

    CPoint* point = new CPoint[40];
    
    ....
    
    	delete point;
    }
    

    delete [] point;



  • CPoint unterstützt die Umwandlung nach POINT, die folgende Variante wird in der MFC meist benutzt:

    //Deklaration:
    void CChildView::DrawGraph(CRect r,const POINT& pt,int pSize)
    //Aufruf:
    CPoint pt(x,y);
    DrawGraph(r,pt,pSize);
    

    Das gleiche gilt auch für CRect, siehe auch GetClientRect.



  • Klappt jetzt. Danke!

    void CChildView::DrawGraph(CRect r,CPoint* p,int pSize)
    {
    	CDC* pDC;
    	GetClientRect(rClient);
    	pDC = (CDC*) GetDC();
    	CPoint* point = new CPoint[pSize];
    	int i,j;		
    	double ymax = 0;
    	double ymin = 0;
    	double hSize = r.right - r.left;
    	double vSize = r.top - r.bottom;
    
    	for(i=0;i<pSize;i++)
    	{
    		if(p[i].y > ymax) ymax = p[i].y;
    		if(p[i].y < ymin) ymin = p[i].y;
    	}
    
    	j=0;
    	for(i=0;i<pSize;i++)
    	{
    		point[j].x=r.left + i * (r.right-r.left) / pSize * 1.025;
    		point[j].y=r.bottom - (r.bottom-r.top)/2 - (p[j].y) * (r.bottom-r.top) / (2 * sqrt(pow(ymax,2)+pow(ymin,2)));
    		j++;
    	}
    
    	char* buffer1 = new char[sizeof(ymax)/sizeof(double)+1];		// must be increased by 1
    	_itoa((ymax/1e6),buffer1,10);
    	pDC->Polyline(point,pSize);
    	pDC->DrawText(buffer1,r,DT_LEFT);
    
    	char* buffer2 = new char[sizeof(ymin)/sizeof(double)+1];		// must be increased by 1
    	r.top = r.bottom*0.96;
    	_itoa((ymin/1e6),buffer2,10);
    	pDC->Polyline(point,pSize);
    	pDC->DrawText(buffer2,r,DT_LEFT);
    
    	delete (point);
    	delete (buffer1);
    	delete (buffer2);
    	ReleaseDC(pDC);
    }
    

Anmelden zum Antworten