Code of Web Version of PixelSort
by James Hall: L6 Age ~17, converted to run in Smart Mobile Studio by PPS
Introduction
Note the following points relating to our adaptations of the code to run in Smart Mobile Studio.- We have changed some of the variable names to be more meaningful.
- With the constant len set to 2, two for loops are unnecessary and have been removed.
- The with keyword is not accepted by Smart, so some of the code looks more cumbersome than the original.
- In the Canvas.StrokeStyle assignment at the end of the program it was necessary to concatenate small strings to build up the colour string.
The Program
unit PixelSortJS; interface uses W3System, W3Components, W3Application, W3Game, W3GameApp, W3Graphics; type Ttrail = record x, y : array[1 .. 200] of integer; colr, colg, colb : integer; realx, realy, targx, targy, speed, angle : real; end; TApplication = class(TW3CustomGameApplication) private const len : integer = 2; num_trails, mouse_x, mouse_y : integer; trails : array[1..10000] of Ttrail; tri : array[1 .. 3, 1 .. 3] of real; mouse_down : Boolean := false; protected procedure ApplicationStarting; override; procedure ApplicationClosing; override; procedure PaintView(Canvas: TW3Canvas); override; procedure AddNewColour; end; implementation procedure TApplication.AddNewColour; begin if num_trails < 5000 then begin inc(num_trails); for var i := 1 to len do begin trails[num_trails].x[i] := mouse_x; trails[num_trails].y[i] := mouse_y; end; trails[num_trails].realx := mouse_x; trails[num_trails].realy := mouse_y; trails[num_trails].colr := trunc(256 * random); trails[num_trails].colg := trunc(256 * random); trails[num_trails].colb := trunc(256 * random); trails[num_trails].speed := 7; trails[num_trails].angle := 0; end; end; procedure TApplication.ApplicationStarting; begin inherited; GameView.Width := 700; GameView.Height := 700; GameView.OnMouseMove := procedure(o : TObject; ss : TShiftState; x, y : integer) begin mouse_x := x; mouse_y := y; if mouse_down then AddNewColour; end; GameView.OnMouseUp := procedure(o : TObject; b : TMouseButton; ss : TShiftState; x, y : integer) begin mouse_down := false; end; GameView.OnMouseDown := procedure(o : TObject; b : TMouseButton; ss : TShiftState; x, y : integer) begin mouse_x := x; mouse_y := y; mouse_down := true; AddNewColour; end; num_trails := 0; tri[1, 1] := 0; tri[2, 1] := 2 * pi / 3; tri[3, 1] := 4 * pi / 3; GameView.Delay := 1; GameView.StartSession(True); end; procedure TApplication.ApplicationClosing; begin GameView.EndSession; inherited; end; procedure TApplication.PaintView(Canvas: TW3Canvas); var r, g, b : integer; begin for var i := 1 to 3 do begin tri[i, 1] := tri[i, 1] + pi / 180; if tri[i, 1] > 2 * pi then tri[i, 1] := tri[i, 1] - 2 * pi; if tri[i, 1] < 0 then tri[i, 1] := tri[i, 1] + 2 * pi; tri[i, 2] := 350 + round(sin(tri[i, 1]) * 200); tri[i, 3] := 350 + round(cos(tri[i, 1]) * 200); end; if num_trails > 0 then for var i := 1 to num_trails do begin trails[i].angle := trails[i].angle + pi / 360; if trails[i].colr + trails[i].colb + trails[i].colg <> 0 then begin trails[i].targx := (trails[i].colr * tri[1, 2] + trails[i].colg * tri[2, 2] + trails[i].colb * tri[3, 2])/(trails[i].colr + trails[i].colb + trails[i].colg); trails[i].targy := (trails[i].colr * tri[1, 3] + trails[i].colg * tri[2, 3] + trails[i].colb * tri[3, 3]) / (trails[i].colr + trails[i].colb + trails[i].colg); end; if (trails[i].targy - trails[i].realy) < 0 then trails[i].angle := arctan((trails[i].targx - trails[i].realx) / (trails[i].targy - trails[i].realy)) + pi; if (trails[i].targy-trails[i].realy) > 0 then trails[i].angle := arctan((trails[i].targx - trails[i].realx) / (trails[i].targy - trails[i].realy)); if ((trails[i].targy - trails[i].realy) = 0) then if (trails[i].targx - trails[i].realx) > 0 then trails[i].angle := pi / 2 else trails[i].angle := 3 * pi / 2; if trails[i].angle > 2 * pi then trails[i].angle := trails[i].angle - 2 * pi; if trails[i].angle < 0 then trails[i].angle := trails[i].angle + 2 * pi; trails[i].realx := trails[i].realx + sin(trails[i].angle) * trails[i].speed; trails[i].realy := trails[i].realy + cos(trails[i].angle) * trails[i].speed; trails[i].x[2] := trails[i].x[1]; trails[i].y[2] := trails[i].y[1]; trails[i].x[1] := round(trails[i].realx); trails[i].y[1] := round(trails[i].realy); r := trails[i].colr; g := trails[i].colg; b := trails[i].colb; Canvas.StrokeStyle := 'rgb(' +intToStr(r)+ ',' + intToStr(g) + ',' + intToStr(b) + ')'; Canvas.BeginPath; Canvas.LineF(trails[i].x[1], trails[i].y[1], trails[i].x[2], trails[i].y[2]); Canvas.Stroke; Canvas.ClosePath; end; end; end.
Remarks
Could you write a similar program constructing objects from polygons?