algorytm.org

Implementacja w C/C++



Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Kreślenie elipsy - Implementacja w C/C++
Ocena użytkownikóww: *****  / 4
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 15 maja 2007 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.

Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.

Elipsa - C++/Unit1.cpp:
//Tomasz Lubiński (C)2007 
//www.algorytm.org
//Algorytm kreslenia elipsy
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void ElipsePoints(int x, int y, int x_move, int y_move)
{
        Form1->Image->Canvas->Pixels[x+x_move][y+y_move] = clBlack;
        Form1->Image->Canvas->Pixels[x+x_move][-y+y_move] = clBlack;
        Form1->Image->Canvas->Pixels[-x+x_move][-y+y_move] = clBlack;
        Form1->Image->Canvas->Pixels[-x+x_move][y+y_move] = clBlack;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
int x, y, limit, d, delta_A, delta_B;
int x_move, y_move, radius_a, radius_b, radius_a2, radius_b2;

x_move = StrToInt(Edit1->Text);
y_move = StrToInt(Edit2->Text);
radius_a = StrToInt(Edit3->Text);
radius_b = StrToInt(Edit4->Text);
radius_a2 = radius_a*radius_a;
radius_b2 = radius_b*radius_b;
d = 4*radius_b2 - 4*radius_b*radius_a2 + radius_a2;
limit   = (radius_a2*radius_a2)/(radius_a2+radius_b2);
x = 0;
y = radius_b;

	while (x*x < limit) {
		ElipsePoints(x,y, x_move,y_move);
		if (d > 0) {
			d += 8*radius_b2*x + 12*radius_b2 - 8*radius_a2*y + 8*radius_a2;
			x += 1;
			y -= 1;
                }
		else {
			d += 8*radius_b2*x + 12*radius_b2;
			x += 1;
                }
        }

radius_a = StrToInt(Edit4->Text);
radius_b = StrToInt(Edit3->Text);
radius_a2 = radius_a*radius_a;
radius_b2 = radius_b*radius_b;
d = 4*radius_b2 - 4*radius_b*radius_a2 + radius_a2;
limit   = (radius_a2*radius_a2)/(radius_a2+radius_b2);
x = 0;
y = radius_b;

	while (x*x < limit) {
		ElipsePoints(y,x, x_move,y_move);
		if (d > 0) {
			d += 8*radius_b2*x + 12*radius_b2 - 8*radius_a2*y + 8*radius_a2;
			x += 1;
			y -= 1;
                }
		else {
			d += 8*radius_b2*x + 12*radius_b2;
			x += 1;
                }
        }
}
//---------------------------------------------------------------------------
Dodaj komentarz