slide: 1 copyright © adacore ten bouncing balls presented by quentin ochem university.adacore.com

32
Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Upload: megan-curtis

Post on 21-Jan-2016

237 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 1Copyright © AdaCore

Ten Bouncing BallsPresented by Quentin Ochem

university.adacore.com

Page 2: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 2Copyright © AdaCore

{X’ = X + DxY’ = Y + Dy}

{X, Y}

Page 3: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 3Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Page 4: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 4Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Checks if a value is within an range

Page 5: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 5Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Moves using a vector

Page 6: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 6Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Get access to random functions

Page 7: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 7Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Initializes a generator to the default

Page 8: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 8Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Value in [0.0 .. 1.0]

Page 9: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 9Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Value in [0.0 .. 0.05]

Page 10: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 10Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Value in [0.02 .. 0.07]

Page 11: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 11Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Value in [0.02 .. 0.07] Value in [0.0 .. 1.0]

Page 12: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 12Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Value in [0.02 .. 0.07] Value in {-1.0, 1.0}

Page 13: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 13Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Value in {[-0.07 .. -0.02], [0.02 .. 0.07]}

Page 14: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 14Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Declares an array type

Page 15: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 15Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

The array in indexed by Integer

Page 16: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 16Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

The array contains a number of elements to be specified at declaration

Page 17: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 17Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

The array contains instances of Ball_Type

Page 18: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 18Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Declare an array of type Ball_Array

Page 19: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 19Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Gives boundaries between 1 and 10 (10 elements)

Page 20: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 20Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Initializes the array through an aggreate

Page 21: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 21Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Every value is initialized through the same expression

Page 22: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 22Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Initializes each component through the same record aggregate

Page 23: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 23Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

These will be recomputed for each element!

Page 24: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 24Copyright © AdaCore

with Display; use Display;with Display.Basic; use Display.Basic;with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure Main is type Ball_Type is record Shape : Shape_Id; Dx, Dy : Float; end record; type Ball_Array is array (Integer range <>) of Ball_Type; Seed : Generator; Balls : Ball_Array (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 10.0, Blue), Dx => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0), Dy => (Random (Seed) * 0.05 + 0.02) * (if Random (Seed) > 0.5 then 1.0 else -1.0)));

procedure Iterate (V : in out Ball_Type) is begin if Get_X (V.Shape) not in -100.0 .. 100.0 then V.Dx := -V.Dx; end if; if Get_Y (V.Shape) not in -100.0 .. 100.0 then V.Dy := -V.Dy; end if; Set_X (V.Shape, Get_X (V.Shape) + V.Dx); Set_Y (V.Shape, Get_Y (V.Shape) + V.Dy); end Iterate; begin loop for B of Balls loop Iterate (B); end loop; delay 0.001; end loop; end Main;

Iterate through each element of Balls

Page 25: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 25Copyright © AdaCore

Quiz

Page 26: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 26Copyright © 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 record;

type Ball_List is array (Integer) of Ball_Type;

List : Ball_List (1 .. 10) := ((Shape => New_Circle (0.0, 0.0, 5.0, Blue), X => 0.0, Y => 0.0, Step => 1.0));begin loop for B in List loop B.X := B.X + B.Step; end loop;

delay 0.001; end loop;end Main;

Page 27: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 27Copyright © 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;

type Ball_List is array (Integer) of Ball_Type;

List : Ball_List (1 .. 10) := ((Shape => New_Circle (0.0, 0.0, 5.0, Blue), X => 0.0, Y => 0.0, Step => 1.0));begin loop for B in List loop B.X := B.X + B.Step; end loop;

delay 0.001; end loop;end Main;

Page 28: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 28Copyright © 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;

type Ball_List is array (Integer) of Ball_Type;

List : Ball_List (1 .. 10) := ((Shape => New_Circle (0.0, 0.0, 5.0, Blue), X => 0.0, Y => 0.0, Step => 1.0));begin loop for B in List loop B.X := B.X + B.Step; end loop;

delay 0.001; end loop;end Main;

“range <>” needs to bespecified for an unconstrained array

Page 29: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 29Copyright © 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;

type Ball_List is array (Integer range <>) of Ball_Type;

List : Ball_List (1 .. 10) := ((Shape => New_Circle (0.0, 0.0, 5.0, Blue), X => 0.0, Y => 0.0, Step => 1.0));begin loop for B in List loop B.X := B.X + B.Step; end loop;

delay 0.001; end loop;end Main;

“others =>” is missing to specifythat a value is givento all objects

Page 30: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 30Copyright © 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;

type Ball_List is array (Integer range <>) of Ball_Type;

List : Ball_List (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 5.0, Blue), X => 0.0, Y => 0.0, Step => 1.0));begin loop for B in List loop B.X := B.X + B.Step; end loop;

delay 0.001; end loop;end Main;

“of” is the notationto iterate over theelements

Page 31: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 31Copyright © 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;

type Ball_List is array (Integer range <>) of Ball_Type;

List : Ball_List (1 .. 10) := (others => (Shape => New_Circle (0.0, 0.0, 5.0, Blue), X => 0.0, Y => 0.0, Step => 1.0));begin loop for B of List loop B.X := B.X + B.Step; end loop;

delay 0.001; end loop;end Main;

Page 32: Slide: 1 Copyright © AdaCore Ten Bouncing Balls Presented by Quentin Ochem university.adacore.com

Slide: 32Copyright © AdaCore

university.adacore.com