rotating balls

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

Upload: maren

Post on 05-Feb-2016

53 views

Category:

Documents


0 download

DESCRIPTION

Rotating Balls. Presented by Quentin Ochem. university.adacore.com. {X = cos (a) * d, Y = sin (a) * d}. d. a. Gives access to sin / cos. Declares a structure of 4 fields. Declares a nested subprogram. The parameter value may be modified. Access to the field Angle of the parameter V. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Rotating Balls

Slide: 1Copyright © AdaCore

Rotating BallsPresented by Quentin Ochem

university.adacore.com

Page 2: Rotating Balls

Slide: 2Copyright © AdaCore

{X = cos (a) * d, Y = sin (a) * d}

a

d

Page 3: Rotating Balls

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

Page 4: Rotating Balls

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

Page 5: Rotating Balls

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

Page 6: Rotating Balls

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

Page 7: Rotating Balls

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

Page 8: Rotating Balls

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

Page 9: Rotating Balls

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

Page 10: Rotating Balls

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

Page 11: Rotating Balls

Slide: 11Copyright © AdaCore

Quiz

Page 12: Rotating Balls

Slide: 12Copyright © AdaCore

Identify the Errorswith 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;

Page 13: Rotating Balls

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

Page 14: Rotating Balls

Slide: 14Copyright © 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 arecord

Page 15: Rotating Balls

Slide: 15Copyright © 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 shouldsay “in out”for modifyingparameter

Page 16: Rotating Balls

Slide: 16Copyright © 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-pointshould be written -1.0

Page 17: Rotating Balls

Slide: 17Copyright © 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 Xand Y aremissing

Page 18: Rotating Balls

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

Page 19: Rotating Balls

Slide: 19Copyright © AdaCore

university.adacore.com