Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide: 1 Copyright © AdaCore Rotating Balls Presented by Quentin Ochem university.adacore.com.

Similar presentations


Presentation on theme: "Slide: 1 Copyright © AdaCore Rotating Balls Presented by Quentin Ochem university.adacore.com."— Presentation transcript:

1 Slide: 1 Copyright © AdaCore Rotating Balls Presented by Quentin Ochem university.adacore.com

2 Slide: 2 Copyright © AdaCore {X = cos (a) * d, Y = sin (a) * d} a d

3 Slide: 3 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; procedure Main is type Ball_Type is record Shape : Shape_Id; Angle_Speed : Float; Angle : Float; Distance : Float; end record; procedure Iterate (V : in out Ball_Type) is begin V.Angle := V.Angle + V.Angle_Speed; Set_X (V.Shape, Cos (V.Angle) * V.Distance); Set_Y (V.Shape, Sin (V.Angle) * V.Distance); end Iterate; B1 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Angle_Speed => 0.001, Angle => 0.0, Distance => 70.0); B2 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Green), Angle_Speed => -0.002, Angle => 0.0, Distance => 40.0); begin loop Iterate (B1); Iterate (B2); delay 0.001; end loop; end Main;

4 Slide: 4 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; procedure Main is type Ball_Type is record Shape : Shape_Id; Angle_Speed : Float; Angle : Float; Distance : Float; end record; procedure Iterate (V : in out Ball_Type) is begin V.Angle := V.Angle + V.Angle_Speed; Set_X (V.Shape, Cos (V.Angle) * V.Distance); Set_Y (V.Shape, Sin (V.Angle) * V.Distance); end Iterate; B1 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Angle_Speed => 0.001, Angle => 0.0, Distance => 70.0); B2 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Green), Angle_Speed => -0.002, Angle => 0.0, Distance => 40.0); begin loop Iterate (B1); Iterate (B2); delay 0.001; end loop; end Main; Gives access to sin / cos

5 Slide: 5 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; procedure Main is type Ball_Type is record Shape : Shape_Id; Angle_Speed : Float; Angle : Float; Distance : Float; end record; procedure Iterate (V : in out Ball_Type) is begin V.Angle := V.Angle + V.Angle_Speed; Set_X (V.Shape, Cos (V.Angle) * V.Distance); Set_Y (V.Shape, Sin (V.Angle) * V.Distance); end Iterate; B1 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Angle_Speed => 0.001, Angle => 0.0, Distance => 70.0); B2 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Green), Angle_Speed => -0.002, Angle => 0.0, Distance => 40.0); begin loop Iterate (B1); Iterate (B2); delay 0.001; end loop; end Main; Declares a structure of 4 fields

6 Slide: 6 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; procedure Main is type Ball_Type is record Shape : Shape_Id; Angle_Speed : Float; Angle : Float; Distance : Float; end record; procedure Iterate (V : in out Ball_Type) is begin V.Angle := V.Angle + V.Angle_Speed; Set_X (V.Shape, Cos (V.Angle) * V.Distance); Set_Y (V.Shape, Sin (V.Angle) * V.Distance); end Iterate; B1 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Angle_Speed => 0.001, Angle => 0.0, Distance => 70.0); B2 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Green), Angle_Speed => -0.002, Angle => 0.0, Distance => 40.0); begin loop Iterate (B1); Iterate (B2); delay 0.001; end loop; end Main; Declares a nested subprogram

7 Slide: 7 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; procedure Main is type Ball_Type is record Shape : Shape_Id; Angle_Speed : Float; Angle : Float; Distance : Float; end record; procedure Iterate (V : in out Ball_Type) is begin V.Angle := V.Angle + V.Angle_Speed; Set_X (V.Shape, Cos (V.Angle) * V.Distance); Set_Y (V.Shape, Sin (V.Angle) * V.Distance); end Iterate; B1 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Angle_Speed => 0.001, Angle => 0.0, Distance => 70.0); B2 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Green), Angle_Speed => -0.002, Angle => 0.0, Distance => 40.0); begin loop Iterate (B1); Iterate (B2); delay 0.001; end loop; end Main; The parameter value may be modified

8 Slide: 8 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; procedure Main is type Ball_Type is record Shape : Shape_Id; Angle_Speed : Float; Angle : Float; Distance : Float; end record; procedure Iterate (V : in out Ball_Type) is begin V.Angle := V.Angle + V.Angle_Speed; Set_X (V.Shape, Cos (V.Angle) * V.Distance); Set_Y (V.Shape, Sin (V.Angle) * V.Distance); end Iterate; B1 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Angle_Speed => 0.001, Angle => 0.0, Distance => 70.0); B2 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Green), Angle_Speed => -0.002, Angle => 0.0, Distance => 40.0); begin loop Iterate (B1); Iterate (B2); delay 0.001; end loop; end Main; Access to the field Angle of the parameter V

9 Slide: 9 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; procedure Main is type Ball_Type is record Shape : Shape_Id; Angle_Speed : Float; Angle : Float; Distance : Float; end record; procedure Iterate (V : in out Ball_Type) is begin V.Angle := V.Angle + V.Angle_Speed; Set_X (V.Shape, Cos (V.Angle) * V.Distance); Set_Y (V.Shape, Sin (V.Angle) * V.Distance); end Iterate; B1 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Angle_Speed => 0.001, Angle => 0.0, Distance => 70.0); B2 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Green), Angle_Speed => -0.002, Angle => 0.0, Distance => 40.0); begin loop Iterate (B1); Iterate (B2); delay 0.001; end loop; end Main; Gives a value by aggregate to an object

10 Slide: 10 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; procedure Main is type Ball_Type is record Shape : Shape_Id; Angle_Speed : Float; Angle : Float; Distance : Float; end record; procedure Iterate (V : in out Ball_Type) is begin V.Angle := V.Angle + V.Angle_Speed; Set_X (V.Shape, Cos (V.Angle) * V.Distance); Set_Y (V.Shape, Sin (V.Angle) * V.Distance); end Iterate; B1 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Angle_Speed => 0.001, Angle => 0.0, Distance => 70.0); B2 : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Green), Angle_Speed => -0.002, Angle => 0.0, Distance => 40.0); begin loop Iterate (B1); Iterate (B2); delay 0.001; end loop; end Main; Calls the nested subprogram on the two objects

11 Slide: 11 Copyright © AdaCore Quiz

12 Slide: 12 Copyright © AdaCore Identify the Errors with Display; use Display; with Display.Basic; use Display.Basic; procedure Main is type Ball_Type is record Shape : Shape_Id; X, Y : Float; Step : Float; end Ball_Type; procedure Iterate (V : Ball_Type) is begin if V.X > 100.0 then V.Step := -1; else V.Step := 1; end if; V.X := V.X + V.Step; end Iterate; B : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Step => 1.0); begin loop Iterate (B); delay 0.001; end loop; end Main;

13 Slide: 13 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; procedure Main is type Ball_Type is record Shape : Shape_Id; X, Y : Float; Step : Float; end Ball_Type; procedure Iterate (V : Ball_Type) is begin if V.X > 100.0 then V.Step := -1; else V.Step := 1; end if; V.X := V.X + V.Step; end Iterate; B : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Step => 1.0); begin loop Iterate (B); delay 0.001; end loop; end Main;

14 Slide: 14 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; procedure Main is type Ball_Type is record Shape : Shape_Id; X, Y : Float; Step : Float; end Ball_Type; procedure Iterate (V : Ball_Type) is begin if V.X > 100.0 then V.Step := -1; else V.Step := 1; end if; V.X := V.X + V.Step; end Iterate; B : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Step => 1.0); begin loop Iterate (B); delay 0.001; end loop; end Main; ‘end record’ closes a record

15 Slide: 15 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; procedure Main is type Ball_Type is record Shape : Shape_Id; X, Y : Float; Step : Float; end record; procedure Iterate (V : Ball_Type) is begin if V.X > 100.0 then V.Step := -1; else V.Step := 1; end if; V.X := V.X + V.Step; end Iterate; B : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Step => 1.0); begin loop Iterate (B); delay 0.001; end loop; end Main; Mode should say “in out” for modifying parameter

16 Slide: 16 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; procedure Main is type Ball_Type is record Shape : Shape_Id; X, Y : Float; Step : Float; end record; procedure Iterate (V : in out Ball_Type) is begin if V.X > 100.0 then V.Step := -1; else V.Step := 1; end if; V.X := V.X + V.Step; end Iterate; B : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Step => 1.0); begin loop Iterate (B); delay 0.001; end loop; end Main; Floating-point should be written -1.0

17 Slide: 17 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; procedure Main is type Ball_Type is record Shape : Shape_Id; X, Y : Float; Step : Float; end record; procedure Iterate (V : in out Ball_Type) is begin if V.X > 100.0 then V.Step := -1.0; else V.Step := 1.0; end if; V.X := V.X + V.Step; end Iterate; B : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Step => 1.0); begin loop Iterate (B); delay 0.001; end loop; end Main; Values for X and Y are missing

18 Slide: 18 Copyright © AdaCore with Display; use Display; with Display.Basic; use Display.Basic; procedure Main is type Ball_Type is record Shape : Shape_Id; X, Y : Float; Step : Float; end record; procedure Iterate (V : in out Ball_Type) is begin if V.X > 100.0 then V.Step := -1.0; else V.Step := 1.0; end if; V.X := V.X + V.Step; end Iterate; B : Ball_Type := (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Step => 1.0, X => 0.0, Y => 0.0); begin loop Iterate (B); delay 0.001; end loop; end Main;

19 Slide: 19 Copyright © AdaCore university.adacore.com


Download ppt "Slide: 1 Copyright © AdaCore Rotating Balls Presented by Quentin Ochem university.adacore.com."

Similar presentations


Ads by Google