Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
169
Task/Simple-turtle-graphics/Ada/simple-turtle-graphics.adb
Normal file
169
Task/Simple-turtle-graphics/Ada/simple-turtle-graphics.adb
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Characters; use Ada.Characters;
|
||||
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
||||
with Ada.Strings; use Ada.Strings;
|
||||
|
||||
-- procedure main - begins program execution
|
||||
procedure main is
|
||||
type Sketch_Pad is array(1 .. 50, 1 .. 50) of Character;
|
||||
thePen : Boolean := True; -- pen raised by default
|
||||
sketch : Sketch_Pad;
|
||||
ycorr, xcorr : Integer := 25;
|
||||
|
||||
-- specifications
|
||||
function penPosition(thePen : in out Boolean) return String;
|
||||
procedure initGrid(sketch : in out Sketch_Pad);
|
||||
procedure commandMenu(thePen : in out Boolean; xcorr : in out Integer;
|
||||
ycorr : in out Integer);
|
||||
procedure showMenu(xcorr : in out Integer; ycorr : in out Integer;
|
||||
thePen : in out Boolean; sketch : in Sketch_Pad);
|
||||
procedure moveCursor(thePen : in Boolean; sketch : in out Sketch_Pad;
|
||||
xcorr : in out Integer; ycorr : in out Integer;
|
||||
ch : in Integer);
|
||||
procedure showGrid(sketch : in Sketch_Pad);
|
||||
|
||||
-- procedure initGrid - creates the sketchpad and initializes elements
|
||||
procedure initGrid(sketch : in out Sketch_Pad) is
|
||||
begin
|
||||
sketch := (others => (others => ' '));
|
||||
end initGrid;
|
||||
|
||||
-- procedure showMenu - displays the menu for the application
|
||||
procedure showMenu(xcorr : in out Integer; ycorr : in out Integer;
|
||||
thePen : in out Boolean; sketch : in Sketch_Pad) is
|
||||
|
||||
choice : Integer := 0;
|
||||
begin
|
||||
while choice /= 4 loop
|
||||
Set_Col(15);
|
||||
Put("TURTLE GRAPHICS APPLICATION");
|
||||
Set_Col(15);
|
||||
Put("===========================");
|
||||
New_Line(2);
|
||||
|
||||
Put_Line("Enter 1 to print the grid map");
|
||||
Put_Line("Enter 2 for command menu");
|
||||
Put_Line("Enter 3 to raise pen up / down");
|
||||
Put_Line("Enter 4 to exit the application");
|
||||
choice := integer'value(Get_Line);
|
||||
|
||||
exit when choice = 4;
|
||||
|
||||
case choice is
|
||||
when 1 => showGrid(sketch);
|
||||
when 2 => commandMenu(thePen, xcorr, ycorr);
|
||||
when 3 => Put_Line("Pen is "
|
||||
& penPosition(thePen));
|
||||
when others => Put_Line("Invalid input");
|
||||
end case;
|
||||
end loop;
|
||||
end showMenu;
|
||||
|
||||
-- function penPosition - checks changes the state of whether the pen is
|
||||
-- raised up or down. If value is True, pen is rasied up
|
||||
function penPosition(thePen : in out Boolean) return String is
|
||||
str1 : constant String := "raised UP";
|
||||
str2 : constant String := "raised DOWN";
|
||||
begin
|
||||
if thePen = True then
|
||||
thePen := False;
|
||||
return str2;
|
||||
else
|
||||
thePen := True;
|
||||
end if;
|
||||
|
||||
return str1;
|
||||
end penPosition;
|
||||
|
||||
-- procedure command menu - provides a list of directions for the turtle
|
||||
-- to move along the grid
|
||||
procedure commandMenu(thePen : in out Boolean; xcorr : in out Integer;
|
||||
ycorr : in out Integer) is
|
||||
|
||||
choice : Integer := 0;
|
||||
begin
|
||||
while choice <= 0 or choice > 5 loop
|
||||
Set_Col(15);
|
||||
Put("Command Menu");
|
||||
Set_Col(15);
|
||||
Put("============");
|
||||
New_Line(2);
|
||||
|
||||
Put_Line("To move North enter 1");
|
||||
Put_Line("To move South enter 2");
|
||||
Put_Line("To move East enter 3");
|
||||
Put_Line("To move West enter 4");
|
||||
Put_Line("To return to previous menu enter 5");
|
||||
choice := integer'value(Get_Line);
|
||||
|
||||
case choice is
|
||||
when 1 => moveCursor(thePen, sketch, xcorr, ycorr, choice);
|
||||
when 2 => moveCursor(thePen, sketch, xcorr, ycorr, choice);
|
||||
when 3 => moveCursor(thePen, sketch, xcorr, ycorr, choice);
|
||||
when 4 => moveCursor(thePen, sketch, xcorr, ycorr, choice);
|
||||
when 5 => showMenu(xcorr, ycorr, thePen, sketch);
|
||||
when others => Put_Line("Invalid choice");
|
||||
end case;
|
||||
end loop;
|
||||
end commandMenu;
|
||||
|
||||
|
||||
-- procedure moveCursor - moves the cursor around the board by taking the
|
||||
-- x and y coordinates from the user. If the pen is down, a character is
|
||||
-- printed at that location. If the pen is up, nothing is printed but the
|
||||
-- cursor still moves to that position
|
||||
procedure moveCursor(thePen : in Boolean; sketch : in out Sketch_Pad;
|
||||
xcorr : in out Integer; ycorr : in out Integer;
|
||||
ch : in Integer) is
|
||||
|
||||
begin
|
||||
if thePen = True then -- pen up so move cursor but do not draw
|
||||
case ch is
|
||||
when 1 => xcorr := xcorr - 1; ycorr := ycorr;
|
||||
sketch(xcorr, ycorr) := ' ';
|
||||
when 2 => xcorr := xcorr + 1; ycorr := ycorr;
|
||||
sketch(xcorr, ycorr) := ' ';
|
||||
when 3 => xcorr := xcorr; ycorr := ycorr + 1;
|
||||
sketch(xcorr, ycorr) := ' ';
|
||||
when 4 => xcorr := xcorr; ycorr := ycorr - 1;
|
||||
sketch(xcorr, ycorr) := ' ';
|
||||
when others => Put("Unreachable Code");
|
||||
end case;
|
||||
|
||||
else -- pen is down so move cursor and draw
|
||||
case ch is
|
||||
when 1 => xcorr := xcorr - 1; ycorr := ycorr;
|
||||
sketch(xcorr, ycorr) := '#';
|
||||
when 2 => xcorr := xcorr + 1; ycorr := ycorr;
|
||||
sketch(xcorr, ycorr) := '#';
|
||||
when 3 => xcorr := xcorr; ycorr := ycorr + 1;
|
||||
sketch(xcorr, ycorr) := '#';
|
||||
when 4 => xcorr := xcorr; ycorr := ycorr - 1;
|
||||
sketch(xcorr, ycorr) := '#';
|
||||
when others => Put("Unreachable Code");
|
||||
end case;
|
||||
end if;
|
||||
end moveCursor;
|
||||
|
||||
-- procedure showGrid - prints the sketchpad showing the plotted moves
|
||||
procedure showGrid(sketch : in Sketch_Pad) is
|
||||
begin
|
||||
New_Line;
|
||||
|
||||
for I in sketch'Range(1) loop
|
||||
for J in sketch'Range(2) loop
|
||||
Put(character'image(sketch(I,J)));
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
New_Line;
|
||||
end showGrid;
|
||||
|
||||
begin
|
||||
New_Line;
|
||||
|
||||
initGrid(sketch);
|
||||
showMenu(xcorr, ycorr, thePen, sketch);
|
||||
|
||||
New_Line;
|
||||
end main;
|
||||
170
Task/Simple-turtle-graphics/Go/simple-turtle-graphics.go
Normal file
170
Task/Simple-turtle-graphics/Go/simple-turtle-graphics.go
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
"slices"
|
||||
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
const (
|
||||
WIDTH = 800
|
||||
HEIGHT = 600
|
||||
)
|
||||
|
||||
type Turtle struct {
|
||||
angle int
|
||||
X int
|
||||
Y int
|
||||
isPenDown bool
|
||||
penColor rl.Color
|
||||
}
|
||||
|
||||
func deg2Rad(degrees int) float64 {
|
||||
return float64(degrees) * math.Pi / 180
|
||||
}
|
||||
|
||||
func (self *Turtle) Right(angle int) {
|
||||
self.angle = (self.angle + angle) % 360
|
||||
}
|
||||
|
||||
func (self *Turtle) Left(angle int) {
|
||||
self.angle = (self.angle - angle) % 360
|
||||
}
|
||||
|
||||
func (self *Turtle) SetPenColor(color rl.Color) {
|
||||
self.penColor = color
|
||||
}
|
||||
|
||||
func (self *Turtle) PenUp() {
|
||||
self.isPenDown = false
|
||||
}
|
||||
|
||||
func (self *Turtle) PenDown() {
|
||||
self.isPenDown = true
|
||||
}
|
||||
|
||||
func (self *Turtle) line(x0, y0, x1, y1 int) {
|
||||
dx := x1 - x0
|
||||
if dx < 0 {
|
||||
dx = -dx
|
||||
}
|
||||
dy := y1 - y0
|
||||
if dy < 0 {
|
||||
dy = -dy
|
||||
}
|
||||
var sx, sy int
|
||||
if x0 < x1 {
|
||||
sx = 1
|
||||
} else {
|
||||
sx = -1
|
||||
}
|
||||
if y0 < y1 {
|
||||
sy = 1
|
||||
} else {
|
||||
sy = -1
|
||||
}
|
||||
err := dx - dy
|
||||
|
||||
for {
|
||||
rl.DrawPixel(int32(x0), int32(y0), self.penColor)
|
||||
if x0 == x1 && y0 == y1 {
|
||||
break
|
||||
}
|
||||
e2 := 2 * err
|
||||
if e2 > -dy {
|
||||
err -= dy
|
||||
x0 += sx
|
||||
}
|
||||
if e2 < dx {
|
||||
err += dx
|
||||
y0 += sy
|
||||
}
|
||||
}
|
||||
}
|
||||
func (self *Turtle) Forward(length int) {
|
||||
endX := self.X - int(math.Round(float64(-length)*math.Cos(deg2Rad(self.angle))))
|
||||
endY := self.Y - int(math.Round(float64(-length)*math.Sin(deg2Rad(self.angle))))
|
||||
|
||||
if self.isPenDown {
|
||||
self.line(self.X, self.Y, endX, endY)
|
||||
}
|
||||
|
||||
self.X = endX
|
||||
self.Y = endY
|
||||
}
|
||||
|
||||
func (self *Turtle) Backward(length int) {
|
||||
self.Forward(-length)
|
||||
}
|
||||
|
||||
func triangle(turtle *Turtle, size int) {
|
||||
for range 3 {
|
||||
turtle.Forward(size)
|
||||
turtle.Right(120)
|
||||
}
|
||||
}
|
||||
|
||||
func rectangle(turtle *Turtle, w int, h int) {
|
||||
for range 2 {
|
||||
turtle.Forward(h)
|
||||
turtle.Left(90)
|
||||
turtle.Forward(w)
|
||||
turtle.Left(90)
|
||||
}
|
||||
}
|
||||
|
||||
func square(turtle *Turtle, size int) {
|
||||
rectangle(turtle, size, size)
|
||||
}
|
||||
|
||||
func house(turtle *Turtle, size int) {
|
||||
turtle.Right(180)
|
||||
square(turtle, size)
|
||||
triangle(turtle, size)
|
||||
turtle.Right(180)
|
||||
}
|
||||
|
||||
func barchart(turtle *Turtle, items []float64, size int) {
|
||||
scale := float64(size) / slices.Max(items)
|
||||
width := size / len(items)
|
||||
|
||||
for _, i := range items {
|
||||
rectangle(turtle, int(i*scale), width)
|
||||
turtle.PenUp()
|
||||
turtle.Forward(width)
|
||||
turtle.PenDown()
|
||||
}
|
||||
|
||||
turtle.PenUp()
|
||||
turtle.Backward(size)
|
||||
turtle.PenDown()
|
||||
}
|
||||
|
||||
func main() {
|
||||
rl.InitWindow(WIDTH, HEIGHT, "Turtle")
|
||||
defer rl.CloseWindow()
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
rl.ClearBackground(rl.Black)
|
||||
rl.BeginDrawing()
|
||||
|
||||
turtle := Turtle{angle: 0, X: WIDTH / 2, Y: HEIGHT / 2, isPenDown: true}
|
||||
turtle.SetPenColor(rl.White)
|
||||
|
||||
house(&turtle, 150)
|
||||
|
||||
turtle.PenUp()
|
||||
turtle.Forward(10)
|
||||
turtle.PenDown()
|
||||
|
||||
barchart(&turtle, []float64{0.5, (1.0 / 3.0), 2.0, 1.3, 0.5}, 200)
|
||||
|
||||
turtle.PenUp()
|
||||
turtle.Backward(10)
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
require "bitmap"
|
||||
|
||||
local w = 600
|
||||
local h = 600
|
||||
local bmp = bitmap.of(w, h, color.white, "Simple_turtle_graphics")
|
||||
local t = turtle.of(bmp)
|
||||
|
||||
local function drawhouse(size)
|
||||
-- Save initial turtle position and direction.
|
||||
local saveX = t.x
|
||||
local saveY = t.y
|
||||
local saveD = t.dir
|
||||
|
||||
t.pen.width = 2
|
||||
|
||||
-- Draw house.
|
||||
t:rect(w // 4, h // 2, size, size)
|
||||
|
||||
-- Draw roof.
|
||||
t:right(30)
|
||||
t:walk(size)
|
||||
t:right(120)
|
||||
t:walk(size)
|
||||
|
||||
-- Draw door.
|
||||
local doorWidth = size // 4
|
||||
local doorHeight = size // 2
|
||||
t:rect(w // 4 + doorWidth // 2, h // 2 + doorHeight, doorWidth, doorHeight)
|
||||
|
||||
-- Draw window
|
||||
local windWidth = size // 3
|
||||
local windHeight = size // 4
|
||||
t:rect(w // 4 + size // 2, h // 2 + size // 2, windWidth, windHeight)
|
||||
|
||||
-- Restore initial turtle position and direction.
|
||||
t.x = saveX
|
||||
t.y = saveY
|
||||
t.dir = saveD
|
||||
end
|
||||
|
||||
-- 'nums' assumed to be all non-negative.
|
||||
local function barChart(nums, size)
|
||||
-- Save intial turtle position and direction.
|
||||
local saveX = t.x
|
||||
local saveY = t.y
|
||||
local saveD = t.dir
|
||||
|
||||
-- Find maximum.
|
||||
local max = nums:max()
|
||||
|
||||
-- Scale to fit within a square with sides 'size' and draw chart.
|
||||
local barWidth = size // #nums
|
||||
local startX = w // 2 + 20
|
||||
local startY = h // 2
|
||||
for i = 1, #nums do
|
||||
local barHeight = math.round(nums[i] * size / max)
|
||||
t:rect(startX, startY - barHeight, barWidth, barHeight)
|
||||
startX += barWidth
|
||||
end
|
||||
|
||||
-- restore intial turtle position and direction
|
||||
t.x = saveX
|
||||
t.y = saveY
|
||||
t.dir = saveD
|
||||
end
|
||||
|
||||
drawhouse(w // 4)
|
||||
barChart({15, 10, 50, 35, 20}, w // 3)
|
||||
bmp:view()
|
||||
Loading…
Add table
Add a link
Reference in a new issue