TRIANGLE DE COULEURS AVEC UTILISATION DU BARYCENTRE

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 - 16 avril 2006 à 17:15
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 - 16 avril 2006 à 17:15
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/18149-triangle-de-couleurs-avec-utilisation-du-barycentre

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
16 avril 2006 à 17:15
en reponse a une question posée il y a deux ans, comment aller plus vite :

pour la methode "Autre" l'affichage est instantané, pour la methode "Barycentre" l'affichage est beaucoup plus rapide.

type

TRGBRec = record
B,G,R : byte;
end;
TRGBRecArray = array[0..32767] of TRGBRec;
pRGBRecArray = ^TRGBRecArray;

procedure TForm1.Button1Click(Sender: TObject);
var
A, B, C, G : TPoint;
IndexRed, IndexGreen, IndexBlue : Integer;
IndexRed2, IndexGreen2, IndexBlue2 : Integer;
IndexX, IndexY : Integer;
Modulo : Integer;

BMP : TBitmap;
ScanLines : pRGBRecArray;
ScanColor : integer;
const
Cote = 622;
Cote255 = 255/Cote;

begin
Effacer;

BMP := TBitmap.Create;
with BMP do begin
Width := ClientWidth;
Height := ClientHeight;
PixelFormat := pf24bit;
Canvas.Brush.Color := color;
Canvas.Pen.Color := color;
Canvas.Rectangle(0,0,ClientWidth,ClientHeight);
end;

A.x := ClientWidth div 2;
A.y := 100;
B.x := A.X - Cote div 2;
B.y := Round(100 + Cote*Sqrt(3) / 2);
C.x := A.X + Cote div 2;
C.y := B.Y;

Case Tracage of
Barycentre : begin
Modulo := StrToInt(Edit1.Text);
for IndexRed := 0 to 255 do begin
for IndexGreen := 0 to 255 do begin
for IndexBlue := 0 to 255 do begin
if (IndexRed+IndexGreen+IndexBlue <> 0) and
(IndexRed mod Modulo = 0) and
(IndexGreen mod Modulo = 0) and
(IndexBlue mod Modulo = 0) then begin
G.x := (IndexRed*A.x + IndexGreen*B.x + IndexBlue*C.x) div (IndexRed+IndexGreen+IndexBlue);
G.y := (IndexRed*A.y + IndexGreen*B.y + IndexBlue*C.y) div (IndexRed+IndexGreen+IndexBlue);
ScanLines := BMP.ScanLine[G.Y];
with ScanLines[G.X] do begin
R := IndexRed;
G := IndexGreen;
B := IndexBlue;
end;
end;
end;
end;
if (indexred mod 10) = 0 then Canvas.Draw(0,0,BMP);
end;
end;

Autre : begin
for IndexX := B.x to C.x do begin
for IndexY := A.y to B.y do begin
IndexRed := Round(sqrt(sqr(IndexX - A.x)+sqr(IndexY - A.y)));
IndexGreen := Round(sqrt(sqr(IndexX - B.x)+sqr(IndexY - B.y)));
IndexBlue := Round(sqrt(sqr(IndexX - C.x)+sqr(IndexY - C.y)));
if (IndexRed < 622) and (IndexGreen < 622) and (IndexBlue < 622) then begin
IndexRed2 := 256 - Round(IndexRed * Cote255);
IndexGreen2 := 256 - Round(IndexGreen * Cote255);
IndexBlue2 := 256 - Round(IndexBlue * Cote255);
ScanLines := BMP.ScanLine[indexY];
with ScanLines[IndexX] do begin
B := IndexBlue2;
G := IndexGreen2;
R := IndexRed2;
end;
end;
end;
end;
end;
end;

Canvas.Draw(0,0,BMP);
BMP.Free;
end;
Rejoignez-nous