Fractal tree: Difference between revisions

Content deleted Content added
Jquorning (talk | contribs)
Ada version
Jquorning (talk | contribs)
Ada: Using renderer for line drawing
Line 17:
with SDL.Video.Windows.Makers;
with SDL.Video.Surfaces;
with SDL.Video.Renderers.Makers;
with SDL.Video.Rectangles;
with SDL.Video.Pixel_Formats;
Line 27 ⟶ 28:
Level : constant := 13;
Length : constant := 130.0;
X_Start : constant := 100475.0;
Y_Start : constant := 20580.0;
A_Start : constant := -1.54;
Angle_1 : constant := 0.10;
Angle_2 : constant := 0.35;
Line 35 ⟶ 36:
C_2 : constant := 0.87;
 
Win Window : SDL.Video.Windows.Window;
Surface : SDL.Video.Surfaces.Surface;
Event Renderer : SDL.EventsVideo.EventsRenderers.EventsRenderer;
Event : SDL.Events.Events.Events;
 
procedure Plot (X, Y : Float) is
use SDL.C;
Point : constant SDL.Video.Rectangles.Rectangle
:= (X => SDL.C.int (X),
Y => Height - SDL.C.int (Y),
Width => 1, Height => 1);
begin
Surface.Fill (Point, SDL.Video.Pixel_Formats.To_Pixel
(Format => Surface.Pixel_Format,
Red => 0, Green => 250, Blue => 0));
end Plot;
 
procedure Draw_Line (X1, Y1, X2, Y2 : Float) is
Step : constant := 0.1;
Alfa : constant Float := (Y2 - Y1) / (X2 - X1);
X, Y : Float;
begin
X := X1; Y := Y1;
while X < X2 loop
Plot (X, Y);
X := X + Step;
Y := Y + Alfa * Step;
end loop;
X := X1; Y := Y1;
while X > X2 loop
Plot (X, Y);
X := X - Step;
Y := Y - Alfa * Step;
end loop;
end Draw_Line;
 
procedure Draw_Tree (Level : in Natural;
Line 75 ⟶ 46:
X, Y : in Float)
is
use SDL.C;
use Ada.Numerics.Elementary_Functions;
Pi : constant := Ada.Numerics.Pi;
X_2 : constant Float := X + Length * Cos (Angle, 2.0 * Pi);
Y_2 : constant Float := Y + Length * Sin (Angle, 2.0 * Pi);
PointLine : constant SDL.Video.Rectangles.RectangleLine_Segment
:= ((C.int (X), C.int (Y)), (C.int (X_2), C.int (Y_2)));
begin
if Level > 0 then
Draw_LineRenderer.Set_Draw_Colour (XColour => (0, Y220, X_20, Y_2255));
Renderer.Draw (Line => Line);
 
Draw_Tree (Level - 1, C_1 * Length, Angle + Angle_1, X_2, Y_2);
Draw_Tree (Level - 1, C_2 * Length, Angle - Angle_2, X_2, Y_2);
Line 105 ⟶ 81:
end if;
 
SDL.Video.Windows.Makers.Create (Win => WinWindow,
Title => "Fractal tree",
Position => SDL.Natural_Coordinates'(X => 10, Y => 10),
Size => SDL.Positive_Sizes'(Width, Height),
Flags => 0);
Surface := WinWindow.Get_Surface;
SDL.Video.Renderers.Makers.Create (Renderer, Surface);
 
Surface.Fill (SDL.Video.Rectangles.Rectangle'(0, 0, Width, Height),
Line 118 ⟶ 95:
 
Draw_Tree (Level, Length, A_Start, X_Start, Y_Start);
WinWindow.Update_Surface;
 
Wait;
WinWindow.Finalize;
SDL.Finalise;
end Fractal_Tree;</lang>