Task: Raster bars




Go: package main

import (
    "image"
    "image/color"
    "image/gif"
    "log"
    "os"
)

var (
    c0 = color.RGBA{166, 124, 0, 255}
    c1 = color.RGBA{191, 155, 48, 255}
    c2 = color.RGBA{255, 191, 0, 255}
    c3 = color.RGBA{255, 207, 64, 255}
    c4 = color.RGBA{255, 220, 115, 255}
)

var palette = []color.Color{c0, c1, c2, c3, c4}

func hline(img *image.Paletted, x1, y, x2 int, ci uint8) {
    for ; x1 <= x2; x1++ {
        img.SetColorIndex(x1, y, ci)
    }
}

func vline(img *image.Paletted, x, y1, y2 int, ci uint8) {
    for ; y1 <= y2; y1++ {
        img.SetColorIndex(x, y1, ci)
    }
}

func drawHbar(img *image.Paletted, x1, y1, x2, y2 int, ci uint8) {
    for ; y1 <= y2; y1++ {
        hline(img, x1, y1, x2, ci)
    }
}

func drawVbar(img *image.Paletted, x1, y1, x2, y2 int, ci uint8) {
    for ; x1 <= x2; x1++ {
        vline(img, x1, y1, y2, ci)
    }
}

func main() {
    const nframes = 40
    const delay = 15 
    width, height := 500, 500
    anim := gif.GIF{LoopCount: 9} 
    rect := image.Rect(0, 0, width, height)

    for f := 0; f < nframes; f++ {
        img := image.NewPaletted(rect, palette)
        c := uint8(f % 2)
        if f < nframes/2 {
            for y := 0; y < height; y += 20 {
                drawHbar(img, 0, y, width, y+19, c)
                c++
                if c == 5 {
                    c = 0
                }
            }
        } else {
            for x := 0; x < width; x += 20 {
                drawVbar(img, x, 0, x+19, height, c)
                c++
                if c == 5 {
                    c = 0
                }
            }
        }
        anim.Delay = append(anim.Delay, delay)
        anim.Image = append(anim.Image, img)
    }

    file, err := os.Create("raster_bars.gif")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()
    if err2 := gif.EncodeAll(file, &anim); err != nil {
        log.Fatal(err2)
    }
}








Ada: with Ada.Numerics.Elementary_Functions;

with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Video.Palettes;
with SDL.Events.Events;

procedure Raster_Bars is

   Width   : constant := 1200;
   Height  : constant := 600;

   Window   : SDL.Video.Windows.Window;
   Renderer : SDL.Video.Renderers.Renderer;

   function Check_Quit return Boolean is
      use type SDL.Events.Event_Types;
      Event : SDL.Events.Events.Events;
   begin
      while SDL.Events.Events.Poll (Event) loop
         if Event.Common.Event_Type = SDL.Events.Quit then
            return True;
         end if;
      end loop;
      return False;
   end Check_Quit;

   procedure Draw_Raster_Bar (Y : Integer) is
      use SDL.Video.Palettes;
      use SDL.C;
      Cc : Colour_Component;
   begin
      for I in 0 .. 10 loop
         Cc := Colour_Component (50 + 20 * I);
         Renderer.Set_Draw_Colour ((Cc, Cc, Cc, 255));
         Renderer.Draw (Line => ((0,     C.int (Y + I)),
                                 (Width, C.int (Y + I))));
      end loop;
   end Draw_Raster_Bar;

   procedure Draw_Raster_Bar_V (X : Integer) is
      use SDL.Video.Palettes;
      use SDL.C;
      Cc : Colour_Component;
   begin
      for I in 0 .. 10 loop
         Cc := Colour_Component (250 - 20 * I);
         Renderer.Set_Draw_Colour ((Cc, Cc, Cc, 255));
         Renderer.Draw (Line => ((C.int (X + I), 0),
                                 (C.int (X + I), Height)));
      end loop;
   end Draw_Raster_Bar_V;

   procedure Raster_Bars is
      use Ada.Numerics.Elementary_Functions;
      Xc  : constant Integer := 800;
      Yc  : constant Integer := 200;
      XA  : constant Float   := 40.0;
      YA  : constant Float   := 30.0;
      N : Float := 0.0;
   begin
      loop
         Renderer.Set_Draw_Colour ((0, 0, 0, 255));
         Renderer.Fill (Rectangle => (0, 0, Width, Height));
         Draw_Raster_Bar (Yc + Integer (YA * Sin (N + 00.0, 360.0)));
         Draw_Raster_Bar (Yc + Integer (YA * Sin (N + 25.0, 360.0)));
         Draw_Raster_Bar (Yc + Integer (YA * Sin (N + 50.0, 360.0)));
         Draw_Raster_Bar (Yc + Integer (YA * Sin (N + 75.0, 360.0)));
         Draw_Raster_Bar_V (Xc + Integer (XA * Sin (0.6 * N + 00.0, 360.0)));
         Draw_Raster_Bar_V (Xc + Integer (XA * Sin (0.6 * N + 90.0, 360.0)));
         Draw_Raster_Bar_V (Xc + Integer (XA * Sin (0.6 * N + 180.0, 360.0)));
         Window.Update_Surface;
         if Check_Quit then
            exit;
         end if;
         delay 0.010;
         N := N + 4.0;
      end loop;
   end Raster_Bars;

begin
   if not SDL.Initialise (Flags => SDL.Enable_Screen) then
      return;
   end if;

   SDL.Video.Windows.Makers.Create (Win      => Window,
                                    Title    => "Raster bar",
                                    Position => SDL.Natural_Coordinates'(X => 10, Y => 10),
                                    Size     => SDL.Positive_Sizes'(Width, Height),
                                    Flags    => 0);
   SDL.Video.Renderers.Makers.Create (Renderer, Window.Get_Surface);

   Raster_Bars;

   Window.Finalize;
   SDL.Finalise;
end Raster_Bars;