Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,36 @@
|
|||
package raster
|
||||
|
||||
// Circle plots a circle with center x, y and radius r.
|
||||
// Limiting behavior:
|
||||
// r < 0 plots no pixels.
|
||||
// r = 0 plots a single pixel at x, y.
|
||||
// r = 1 plots four pixels in a diamond shape around the center pixel at x, y.
|
||||
func (b *Bitmap) Circle(x, y, r int, p Pixel) {
|
||||
if r < 0 {
|
||||
return
|
||||
}
|
||||
// Bresenham algorithm
|
||||
x1, y1, err := -r, 0, 2-2*r
|
||||
for {
|
||||
b.SetPx(x-x1, y+y1, p)
|
||||
b.SetPx(x-y1, y-x1, p)
|
||||
b.SetPx(x+x1, y-y1, p)
|
||||
b.SetPx(x+y1, y+x1, p)
|
||||
r = err
|
||||
if r > x1 {
|
||||
x1++
|
||||
err += x1*2 + 1
|
||||
}
|
||||
if r <= y1 {
|
||||
y1++
|
||||
err += y1*2 + 1
|
||||
}
|
||||
if x1 >= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bitmap) CircleRgb(x, y, r int, c Rgb) {
|
||||
b.Circle(x, y, r, c.Pixel())
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
// Files required to build supporting package raster are found in:
|
||||
// * This task (immediately above)
|
||||
// * Bitmap
|
||||
// * Write a PPM file
|
||||
|
||||
import (
|
||||
"raster"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := raster.NewBitmap(400, 300)
|
||||
b.FillRgb(0xffdf20) // yellow
|
||||
// large circle, demonstrating clipping to image boundaries
|
||||
b.CircleRgb(300, 249, 200, 0xff2020) // red
|
||||
if err := b.WritePpmFile("circle.ppm"); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue