delphi - Graphics32 fill polygon with hatched pattern -


i trying convert delphi xe4 application use graphics32 libraries drawing rather standard delphi drawing methods.

one thing draw icon contains small ellipse diagonal cross hatch pattern. icon should this:

enter image description here

here how standard tcanvas drawing methods:

acanvas.brush.color := shape.pcolor; acanvas.brush.style := bsdiagcross; acanvas.ellipse(-13, -9, 13, 9); 

i can draw ellipse graphics32 doing following:

var       polygon : tarrayoffloatpoint;    begin       polygon := ellipse(0, 0, 13, 9);   polylinefs(bitmap, polygon, pcolor, true, uavpenwidth); 

but there easy way replicate diagonal cross hatching pattern? assume can use tbitmappolygonfiller class fill using bitmap. note drawing tpositionedlayer in it's onpaint event handler if relevant.

so far there no direct pattern support in graphics32, there dozens of ways create patterns 1 want use.

here's 1 solution using sample polygon filler:

first need write sampler class hatched pattern. there several ways build such sampler. below can find simple one:

type   thatchedpatternsampler = class(tcustomsampler)   public     function getsampleint(x, y: integer): tcolor32; override;   end;  function thatchedpatternsampler.getsampleint(x, y: integer): tcolor32; begin   result := 0;   if ((x - y) mod 8 = 0) or ((x + y) mod 8 = 0)     result := clred32 end; 

you need override 1 method here (getsampleint), other methods can used ancestor class.

now gets little bit convolved. in order use sample must use on top of tsamplerfiller this:

sampler := thatchedpatternsampler.create; filler := tsamplerfiller.create(sampler); 

once have filler can use in polygonfs or polylinefs.

finally code may this:

var   polygon: tarrayoffloatpoint;   sampler: thatchedpatternsampler;   filler: tsamplerfiller; begin   polygon := ellipse(128, 128, 120, 100);   sampler := thatchedpatternsampler.create;   try     filler := tsamplerfiller.create(sampler);     try       polygonfs(paintbox32.buffer, polygon, filler);           filler.free;     end;           sampler.free;   end;    polylinefs(paintbox32.buffer, polygon, clred32, true, 1); end; 

this draw rather big ellipse center of bitmap (here: buffer of tpaintbox32 instance) , fill hatched sampler code. solid outline drawn using polylinefs function.

from performance perspective isn't fastest approach getsampleint gets called per pixel. however, it's easiest understand happens.

for faster alternative should directly use filler directly. can derive directly tcustompolygonfiller this:

type   thatchedpatternfiller = class(tcustompolygonfiller)   private     procedure fillline(dst: pcolor32; dstx, dsty, length: integer; alphavalues: pcolor32);   protected     function getfillline: tfilllineevent; override;   end; 

where method getfillline gets simple as:

function thatchedpatternfiller.getfillline: tfilllineevent; begin   result := fillline; end; 

however, fillline method bit more complex this:

procedure thatchedpatternfiller.fillline(dst: pcolor32; dstx, dsty,   length: integer; alphavalues: pcolor32); var   x: integer; begin   x := dstx dstx + length   begin     if ((x - dsty) mod 8 = 0) or ((x + dsty) mod 8 = 0)       dst^ :=clred32     else       dst^ := 0;      inc(dst);   end; end; 

since dsty remains constant refactor code improve performance. or speed code using assembler (sse), guess overkill such simple function.


Comments

Popular posts from this blog

html - How to custom Bootstrap grid height? -

javascript - pass values from mssql to views in node -

javascript - Understanding ExtJS Framework Syntax -