Sinus Kurve in dos box ausgeben



  • wenn du nur ansi c ausgabe hast, dann musst du die ausgabe in einem 2d array vorpuffern.
    wenn du aber conio.h oder vergleichbares hast (gotoxy, clrscr, ...), dann kannst du dir das auch sparen und gleich die kurve malen.



  • ich muss das für die schule machen, und da hatten wir noch gar keine Arrays und conio.h sowieso nicht. 😞

    ich muss das einfach mit ner schlaufe und der sin() funktion lösen, jedoch klappt das noch nicht wirklich bis jetzt!



  • kann das sein, dass ihr nicht "bildlich" ausgeben sollt, sondern durch ein nährungsverfahren (zum bleistift newton) http://de.wikipedia.org/wiki/Newtonsches_Näherungsverfahren den sinus herleiten sollt?

    edit: war unsinn... denn du sagst, ihr sollt mit '*' malen

    ich denke, ihr sollt tatsächlich MALEN



  • hier in c++ (keine lust, es umzuduckseln)

    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    #include <cmath>
    
    void gotoxy(const int y, const int x);
    
    #define PI      3.1415926
    #define START   0.0
    #define ENDE     (2.0 * PI)
    
    #define PKT     64
    #define SCHRITT ((ENDE - START) / PKT)
    #define xA      12
    #define yA      10
    
    int main()
    {
        int zeile, spalte;
    
        gotoxy(2,25);
    
        cout << "------- Die Sinus Funktion -------";
    
        cout << "\n\n";
    
        gotoxy(xA, 1);
        for(spalte = 1; spalte < 78; ++spalte)
        {
            cout << ((spalte - yA) % 8 ? "\304" : "\305");
        }
        cout << "\020";
        gotoxy(xA - 1, yA + 64);
        cout << "2PI x";
    
        for (zeile = 5; zeile < 24; ++zeile)
        {
            gotoxy(zeile, yA);
            cout << "\305";
        }
    
        gotoxy(4,yA);
        cout << "\036 sin(x)";
        gotoxy(xA - 8, yA + 1); cout << " 1";
        gotoxy(xA + 8, yA + 1); cout << " -1";
    
        int anfsp = yA, endsp = anfsp + PKT;
    
        for(spalte = anfsp; spalte <= endsp; ++spalte)
        {
            double x = (spalte - yA) * SCHRITT;
            zeile = (int)(xA - 8 * sin(x) + 0.5);
            gotoxy(zeile, spalte); cout << "*";
        }
    
        gotoxy(25,1);
    }
    
    void gotoxy(const int y, const int x){
       COORD coords;
    
       HANDLE Hnd = GetStdHandle(STD_OUTPUT_HANDLE);
       if (Hnd == INVALID_HANDLE_VALUE)
          return;
    
       coords.X = (x - 1);
       coords.Y = (y - 1);
       SetConsoleCursorPosition(Hnd, coords);
    }
    




  • #include <iostream>
    #include <cmath>
    using namespace std;
    
    class Graphic
    {
    public:
    	Graphic(int width, int height)
    	{
    		FrameBuffer = new char[width * height];
    
    		for(int i = 0; i < width * height; i++)
    			FrameBuffer[i] = '0';
    
    		this->width = width;
    		this->height = height;
    	}
    
    	~Graphic()
    	{
    		delete [] FrameBuffer;
    	}
    
    	int RHtoLH(int y)
    	{
    		return height - y - 1;
    	}
    
    	void Draw()
    	{
    		for(int i = 0; i < width * height; i++)
    			cout<<FrameBuffer[i];
    	}
    
    	void SetPixelRH(int x, int y)
    	{
    		y = RHtoLH(y);
    
    		if(x >= width || x < 0 || y < 0 || y >= height) // clipping
    			return;
    
    		FrameBuffer[x+y*width] = '*';
    	}
    
    	void Bresenham(int d1x, int d1y, int d2x, int d2y) 
    	{    
    		bool Z = true;     // zur Bestimmung der Bewegunsrichtung 
    		int *m;            // Bewegungsrichtung/Vektoren 
    
    		// delta a, delta b und Bewegungsrichtung bestimmen 
    		int dx = d2x - d1x; 
    		int dy = d2y - d1y; 
    
    		int da = abs(dx), db = abs(dy); 
    
    		if(da-db<0)
    		{
    			int tmp = da;
    			da = db;
    			db = tmp;
    			Z = false;
    		}
    
    		bool X = (dx >= 0); 
    		bool Y = (dy >= 0); 
    
    		int array[8][4] = 
    		{ 
    			{ 0,-1,-1,-1 },        
    			{ 0,-1,1,-1 },        
    			{ 0,1,-1,1 },        
    			{ 0,1,1,1 },        
    			{ -1,0,-1,-1 },        
    			{ 1,0,1,-1 },        
    			{ -1,0,-1,1 },        
    			{ 1,0,1,1 },        
    		}; 
    
    		m = array[(X?1:0) + (Y?2:0) + (Z?4:0)]; 
    
    		int gradient = 2 * db - da; 
    		int x = d1x; 
    		int y = d1y; 
    
    		SetPixelRH(x, y); 
    
    		for(int i = 0; i < da; i++) 
    		{ 
    			if(gradient >= 0) 
    			{ 
    				x = x + m[2]; 
    				y = y + m[3]; 
    				gradient = gradient + 2 * db - 2 * da; 
    			} 
    			else 
    			{ 
    				x = x + m[0]; 
    				y = y + m[1]; 
    				gradient = gradient + 2 * db;
    			} 
    
    			SetPixelRH(x, y); 
    		} 
    	} 
    
    private:
    
    	// Der Ursprung des Frame Buffer liegt in der linken oberen Ecke
    	char *FrameBuffer;
    	int width;
    	int height;
    };
    
    int main()
    {
    	Graphic MyGraphic(80, 24);
    
    	MyGraphic.Bresenham(0, 12, 80, 12);
    	MyGraphic.Bresenham(40, 0, 40, 24);
    
    	// Der Nullpunkt liegt bei 12, 40
    	// nach oben und unten sind 11 Pixel Platz
    
    	const double PI = 3.14;
    
    	for(double x = 0; x < 2*PI; x+=0.3)
    	{
    		// welche y Koordinate hat der Punkt x?
    
    		int y = 11.0 * sin(double(x+PI))+12;
    		int y2 = 11.0 * sin(double(x+PI+0.3))+12;
    
    		MyGraphic.Bresenham(double(x)*80/(2*PI),y,double(x+0.3)*80/(2*PI),y2);
    	}
    
    	MyGraphic.Draw();
    
    	system("pause");
    }
    


  • auch das ist c++ *lach*



  • in 2 minuten wandle ich den Code in ein C# Programm um 😉 - muss nur den Zeiger auf den FrameBuffer ändern, Destruktor löschen - cout durch System.Console.Write() ersetzen 😉

    PS: irgendeine arbeit müssen wir ihm doch noch lassen 😉



  • aber ansi c ist schwerer 😉



  • ich könnte sogar ein Codekonverter programmieren, der den C++ Code in Ansi C konvertiert 😉 (alte Compiler machen das ja auch so)

    objektorientiert programmieren mit c - in oop für dummies gibts dazu ein ganzes kapitel


Anmelden zum Antworten