2018-07-22 00:12:22 +00:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-27 18:57:26 +00:00
|
|
|
"image"
|
2018-07-24 03:10:53 +00:00
|
|
|
"math"
|
2018-07-22 00:12:22 +00:00
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/lib/events"
|
2018-07-22 00:12:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Engine is the interface for the rendering engine, keeping SDL-specific stuff
|
|
|
|
// far away from the core of Doodle.
|
|
|
|
type Engine interface {
|
|
|
|
Setup() error
|
|
|
|
|
|
|
|
// Poll for events like keypresses and mouse clicks.
|
|
|
|
Poll() (*events.State, error)
|
|
|
|
GetTicks() uint32
|
2018-10-19 20:31:58 +00:00
|
|
|
WindowSize() (w, h int)
|
2018-07-22 00:12:22 +00:00
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
// Present presents the current state to the screen.
|
|
|
|
Present() error
|
2018-07-22 00:12:22 +00:00
|
|
|
|
|
|
|
// Clear the full canvas and set this color.
|
|
|
|
Clear(Color)
|
2019-06-25 21:57:11 +00:00
|
|
|
SetTitle(string)
|
2018-07-22 00:12:22 +00:00
|
|
|
DrawPoint(Color, Point)
|
|
|
|
DrawLine(Color, Point, Point)
|
|
|
|
DrawRect(Color, Rect)
|
2018-07-22 03:43:01 +00:00
|
|
|
DrawBox(Color, Rect)
|
|
|
|
DrawText(Text, Point) error
|
2018-07-25 16:03:49 +00:00
|
|
|
ComputeTextRect(Text) (Rect, error)
|
2018-07-22 00:12:22 +00:00
|
|
|
|
WIP Texture Caching
NOTICE: Chunk size set to 100 for visual testing!
NOTICE: guitest references a bmp file that isn't checked in!
BUGS REMAINING:
- When scrolling the level in Edit Mode, some of the chunks will pop
out of existence randomly.
- When clicking-dragging to draw in Edit Mode, if the scroll position
is not at 0,0 then the pixels drawn will be offset from the cursor.
- These are to do with the Scroll position and chunk coordinate calc
functions probably.
Implements a texture caching interface to stop redrawing everything
pixel by pixel on every frame.
The texture caching workflow is briefly:
- The uix.Canvas widget's Present() function iterates over the list of
Chunk Coordinates that are visible inside of the current viewport
(i.e. viewable on screen)
- For each Chunk:
- Make it render and/or return its cached Texture object.
- Work out how much of the Chunk will be visible and how to crop the
boxes for the Copy()
- Copy the cached Texture instead of drawing all the pixels every
time like we were doing before.
- The Chunk.Texture() function that returns said Texture:
- It calls Chunk.ToBitmap() to save a bitmap on disk.
- It calls Engine.NewBitmap() to get a Texture it can hang onto.
- It hangs onto the Texture and returns it on future calls.
- Any call to Set() or Delete() a pixel will invalidate the cache
(mark the Chunk "dirty") and Texture() will rebuild next call.
The interface `render.Texturer` provides a way for rendering backends
(SDL2, OpenGL) to transport a "texture" of their own kind without
exposing the type details to the user.
The interface `render.Engine` adds two new methods:
* NewBitmap(filename string) (Texturer, error)
* Copy(t Texturer, src, dst Rect)
NewBitmap should open a bitmap image on disk and return it wrapped in a
Texturer (really it's an SDL2 Texture). This is for caching purposes.
Next the Copy() function blits the texture onto the screen renderer
using the source and destination rectangles.
The uix.Canvas widget orchestrates the caching for the drawing it's
responsible for. It queries which chunks are viewable in the Canvas
viewport (scroll and bounding boxes), has each chunk render out their
entire bitmap image to then cache them as SDL textures and then only
_those_ need to be copied out to the renderer each frame.
The frame rate now sits at a decent 60 FPS even when the drawing gets
messy and full of lines. Each unique version of each chunk needs to
render only one time and then it's a fast copy operation for future
ticks.
Other changes:
- Chunker now assigns each Chunk what their coordinate and size are, so
that the chunk can self reference that information. This info is
considered read-only but that isn't really enforced.
- Add Chunker.IterViewportChunks() that returns a channel of Chunk
Coordinates that are visible in your viewport, rather than iterating
over all of the pixels in all of those chunks.
- Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels
to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture
caching. Currently these go to files in /tmp but will soon go into your
$XDG_CACHE_FOLDER instead.
- Add Chunk.Texture() that causes a Chunk to render and then return a
cached bitmap texture of the pixels it's responsible for. The texture
is cached until the Chunk is next modified with Set() or Delete().
- UI: add an Image widget that currently just shows a bitmap image. It
was the first test for caching bitmap images for efficiency. Can show
any *.bmp file on disk!
- Editor UI: make the StatusBar boxes dynamically build from an array
of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
|
|
|
// Texture caching.
|
2019-06-27 20:01:01 +00:00
|
|
|
StoreTexture(name string, img image.Image) (Texturer, error)
|
|
|
|
LoadTexture(name string) (Texturer, error)
|
WIP Texture Caching
NOTICE: Chunk size set to 100 for visual testing!
NOTICE: guitest references a bmp file that isn't checked in!
BUGS REMAINING:
- When scrolling the level in Edit Mode, some of the chunks will pop
out of existence randomly.
- When clicking-dragging to draw in Edit Mode, if the scroll position
is not at 0,0 then the pixels drawn will be offset from the cursor.
- These are to do with the Scroll position and chunk coordinate calc
functions probably.
Implements a texture caching interface to stop redrawing everything
pixel by pixel on every frame.
The texture caching workflow is briefly:
- The uix.Canvas widget's Present() function iterates over the list of
Chunk Coordinates that are visible inside of the current viewport
(i.e. viewable on screen)
- For each Chunk:
- Make it render and/or return its cached Texture object.
- Work out how much of the Chunk will be visible and how to crop the
boxes for the Copy()
- Copy the cached Texture instead of drawing all the pixels every
time like we were doing before.
- The Chunk.Texture() function that returns said Texture:
- It calls Chunk.ToBitmap() to save a bitmap on disk.
- It calls Engine.NewBitmap() to get a Texture it can hang onto.
- It hangs onto the Texture and returns it on future calls.
- Any call to Set() or Delete() a pixel will invalidate the cache
(mark the Chunk "dirty") and Texture() will rebuild next call.
The interface `render.Texturer` provides a way for rendering backends
(SDL2, OpenGL) to transport a "texture" of their own kind without
exposing the type details to the user.
The interface `render.Engine` adds two new methods:
* NewBitmap(filename string) (Texturer, error)
* Copy(t Texturer, src, dst Rect)
NewBitmap should open a bitmap image on disk and return it wrapped in a
Texturer (really it's an SDL2 Texture). This is for caching purposes.
Next the Copy() function blits the texture onto the screen renderer
using the source and destination rectangles.
The uix.Canvas widget orchestrates the caching for the drawing it's
responsible for. It queries which chunks are viewable in the Canvas
viewport (scroll and bounding boxes), has each chunk render out their
entire bitmap image to then cache them as SDL textures and then only
_those_ need to be copied out to the renderer each frame.
The frame rate now sits at a decent 60 FPS even when the drawing gets
messy and full of lines. Each unique version of each chunk needs to
render only one time and then it's a fast copy operation for future
ticks.
Other changes:
- Chunker now assigns each Chunk what their coordinate and size are, so
that the chunk can self reference that information. This info is
considered read-only but that isn't really enforced.
- Add Chunker.IterViewportChunks() that returns a channel of Chunk
Coordinates that are visible in your viewport, rather than iterating
over all of the pixels in all of those chunks.
- Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels
to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture
caching. Currently these go to files in /tmp but will soon go into your
$XDG_CACHE_FOLDER instead.
- Add Chunk.Texture() that causes a Chunk to render and then return a
cached bitmap texture of the pixels it's responsible for. The texture
is cached until the Chunk is next modified with Set() or Delete().
- UI: add an Image widget that currently just shows a bitmap image. It
was the first test for caching bitmap images for efficiency. Can show
any *.bmp file on disk!
- Editor UI: make the StatusBar boxes dynamically build from an array
of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
|
|
|
Copy(t Texturer, src, dst Rect)
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
// Delay for a moment using the render engine's delay method,
|
|
|
|
// implemented by sdl.Delay(uint32)
|
|
|
|
Delay(uint32)
|
|
|
|
|
|
|
|
// Tasks that the Setup function should defer until tear-down.
|
|
|
|
Teardown()
|
|
|
|
|
|
|
|
Loop() error // maybe?
|
|
|
|
}
|
|
|
|
|
WIP Texture Caching
NOTICE: Chunk size set to 100 for visual testing!
NOTICE: guitest references a bmp file that isn't checked in!
BUGS REMAINING:
- When scrolling the level in Edit Mode, some of the chunks will pop
out of existence randomly.
- When clicking-dragging to draw in Edit Mode, if the scroll position
is not at 0,0 then the pixels drawn will be offset from the cursor.
- These are to do with the Scroll position and chunk coordinate calc
functions probably.
Implements a texture caching interface to stop redrawing everything
pixel by pixel on every frame.
The texture caching workflow is briefly:
- The uix.Canvas widget's Present() function iterates over the list of
Chunk Coordinates that are visible inside of the current viewport
(i.e. viewable on screen)
- For each Chunk:
- Make it render and/or return its cached Texture object.
- Work out how much of the Chunk will be visible and how to crop the
boxes for the Copy()
- Copy the cached Texture instead of drawing all the pixels every
time like we were doing before.
- The Chunk.Texture() function that returns said Texture:
- It calls Chunk.ToBitmap() to save a bitmap on disk.
- It calls Engine.NewBitmap() to get a Texture it can hang onto.
- It hangs onto the Texture and returns it on future calls.
- Any call to Set() or Delete() a pixel will invalidate the cache
(mark the Chunk "dirty") and Texture() will rebuild next call.
The interface `render.Texturer` provides a way for rendering backends
(SDL2, OpenGL) to transport a "texture" of their own kind without
exposing the type details to the user.
The interface `render.Engine` adds two new methods:
* NewBitmap(filename string) (Texturer, error)
* Copy(t Texturer, src, dst Rect)
NewBitmap should open a bitmap image on disk and return it wrapped in a
Texturer (really it's an SDL2 Texture). This is for caching purposes.
Next the Copy() function blits the texture onto the screen renderer
using the source and destination rectangles.
The uix.Canvas widget orchestrates the caching for the drawing it's
responsible for. It queries which chunks are viewable in the Canvas
viewport (scroll and bounding boxes), has each chunk render out their
entire bitmap image to then cache them as SDL textures and then only
_those_ need to be copied out to the renderer each frame.
The frame rate now sits at a decent 60 FPS even when the drawing gets
messy and full of lines. Each unique version of each chunk needs to
render only one time and then it's a fast copy operation for future
ticks.
Other changes:
- Chunker now assigns each Chunk what their coordinate and size are, so
that the chunk can self reference that information. This info is
considered read-only but that isn't really enforced.
- Add Chunker.IterViewportChunks() that returns a channel of Chunk
Coordinates that are visible in your viewport, rather than iterating
over all of the pixels in all of those chunks.
- Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels
to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture
caching. Currently these go to files in /tmp but will soon go into your
$XDG_CACHE_FOLDER instead.
- Add Chunk.Texture() that causes a Chunk to render and then return a
cached bitmap texture of the pixels it's responsible for. The texture
is cached until the Chunk is next modified with Set() or Delete().
- UI: add an Image widget that currently just shows a bitmap image. It
was the first test for caching bitmap images for efficiency. Can show
any *.bmp file on disk!
- Editor UI: make the StatusBar boxes dynamically build from an array
of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
|
|
|
// Texturer is a stored image texture used by the rendering engine while
|
|
|
|
// abstracting away its inner workings.
|
|
|
|
type Texturer interface {
|
|
|
|
Size() Rect
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
// Rect has a coordinate and a width and height.
|
|
|
|
type Rect struct {
|
|
|
|
X int32
|
|
|
|
Y int32
|
|
|
|
W int32
|
|
|
|
H int32
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:52:52 +00:00
|
|
|
// NewRect creates a rectangle of size `width` and `height`. The X,Y values
|
|
|
|
// are initialized to zero.
|
|
|
|
func NewRect(width, height int32) Rect {
|
|
|
|
return Rect{
|
|
|
|
W: width,
|
|
|
|
H: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
func (r Rect) String() string {
|
|
|
|
return fmt.Sprintf("Rect<%d,%d,%d,%d>",
|
|
|
|
r.X, r.Y, r.W, r.H,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
// Point returns the rectangle's X,Y values as a Point.
|
|
|
|
func (r Rect) Point() Point {
|
|
|
|
return Point{
|
|
|
|
X: r.X,
|
|
|
|
Y: r.Y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:52:52 +00:00
|
|
|
// Bigger returns if the given rect is larger than the current one.
|
|
|
|
func (r Rect) Bigger(other Rect) bool {
|
|
|
|
// TODO: don't know why this is !
|
|
|
|
return !(other.X < r.X || // Lefter
|
|
|
|
other.Y < r.Y || // Higher
|
|
|
|
other.W > r.W || // Wider
|
|
|
|
other.H > r.H) // Taller
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// Intersects with the other rectangle in any way.
|
|
|
|
func (r Rect) Intersects(other Rect) bool {
|
|
|
|
// Do a bidirectional compare.
|
|
|
|
compare := func(a, b Rect) bool {
|
|
|
|
var corners = []Point{
|
|
|
|
NewPoint(b.X, b.Y),
|
|
|
|
NewPoint(b.X, b.Y+b.H),
|
|
|
|
NewPoint(b.X+b.W, b.Y),
|
|
|
|
NewPoint(b.X+b.W, b.Y+b.H),
|
|
|
|
}
|
|
|
|
for _, pt := range corners {
|
|
|
|
if pt.Inside(a) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return compare(r, other) || compare(other, r) || false
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:52:52 +00:00
|
|
|
// IsZero returns if the Rect is uninitialized.
|
|
|
|
func (r Rect) IsZero() bool {
|
|
|
|
return r.X == 0 && r.Y == 0 && r.W == 0 && r.H == 0
|
|
|
|
}
|
|
|
|
|
WIP Texture Caching
NOTICE: Chunk size set to 100 for visual testing!
NOTICE: guitest references a bmp file that isn't checked in!
BUGS REMAINING:
- When scrolling the level in Edit Mode, some of the chunks will pop
out of existence randomly.
- When clicking-dragging to draw in Edit Mode, if the scroll position
is not at 0,0 then the pixels drawn will be offset from the cursor.
- These are to do with the Scroll position and chunk coordinate calc
functions probably.
Implements a texture caching interface to stop redrawing everything
pixel by pixel on every frame.
The texture caching workflow is briefly:
- The uix.Canvas widget's Present() function iterates over the list of
Chunk Coordinates that are visible inside of the current viewport
(i.e. viewable on screen)
- For each Chunk:
- Make it render and/or return its cached Texture object.
- Work out how much of the Chunk will be visible and how to crop the
boxes for the Copy()
- Copy the cached Texture instead of drawing all the pixels every
time like we were doing before.
- The Chunk.Texture() function that returns said Texture:
- It calls Chunk.ToBitmap() to save a bitmap on disk.
- It calls Engine.NewBitmap() to get a Texture it can hang onto.
- It hangs onto the Texture and returns it on future calls.
- Any call to Set() or Delete() a pixel will invalidate the cache
(mark the Chunk "dirty") and Texture() will rebuild next call.
The interface `render.Texturer` provides a way for rendering backends
(SDL2, OpenGL) to transport a "texture" of their own kind without
exposing the type details to the user.
The interface `render.Engine` adds two new methods:
* NewBitmap(filename string) (Texturer, error)
* Copy(t Texturer, src, dst Rect)
NewBitmap should open a bitmap image on disk and return it wrapped in a
Texturer (really it's an SDL2 Texture). This is for caching purposes.
Next the Copy() function blits the texture onto the screen renderer
using the source and destination rectangles.
The uix.Canvas widget orchestrates the caching for the drawing it's
responsible for. It queries which chunks are viewable in the Canvas
viewport (scroll and bounding boxes), has each chunk render out their
entire bitmap image to then cache them as SDL textures and then only
_those_ need to be copied out to the renderer each frame.
The frame rate now sits at a decent 60 FPS even when the drawing gets
messy and full of lines. Each unique version of each chunk needs to
render only one time and then it's a fast copy operation for future
ticks.
Other changes:
- Chunker now assigns each Chunk what their coordinate and size are, so
that the chunk can self reference that information. This info is
considered read-only but that isn't really enforced.
- Add Chunker.IterViewportChunks() that returns a channel of Chunk
Coordinates that are visible in your viewport, rather than iterating
over all of the pixels in all of those chunks.
- Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels
to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture
caching. Currently these go to files in /tmp but will soon go into your
$XDG_CACHE_FOLDER instead.
- Add Chunk.Texture() that causes a Chunk to render and then return a
cached bitmap texture of the pixels it's responsible for. The texture
is cached until the Chunk is next modified with Set() or Delete().
- UI: add an Image widget that currently just shows a bitmap image. It
was the first test for caching bitmap images for efficiency. Can show
any *.bmp file on disk!
- Editor UI: make the StatusBar boxes dynamically build from an array
of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
|
|
|
// Add another rect.
|
|
|
|
func (r Rect) Add(other Rect) Rect {
|
|
|
|
return Rect{
|
|
|
|
X: r.X + other.X,
|
|
|
|
Y: r.Y + other.Y,
|
|
|
|
W: r.W + other.W,
|
|
|
|
H: r.H + other.H,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a point to move the rect.
|
|
|
|
func (r Rect) AddPoint(other Point) Rect {
|
|
|
|
return Rect{
|
|
|
|
X: r.X + other.X,
|
|
|
|
Y: r.Y + other.Y,
|
|
|
|
W: r.W,
|
|
|
|
H: r.H,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 22:25:03 +00:00
|
|
|
// SubtractPoint is the inverse of AddPoint. Use this only if you need to invert
|
|
|
|
// the Point being added.
|
|
|
|
//
|
|
|
|
// This does r.X - other.X, r.Y - other.Y and keeps the width/height the same.
|
|
|
|
func (r Rect) SubtractPoint(other Point) Rect {
|
|
|
|
return Rect{
|
|
|
|
X: r.X - other.X,
|
|
|
|
Y: r.Y - other.Y,
|
|
|
|
W: r.W,
|
|
|
|
H: r.H,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
// Text holds information for drawing text.
|
|
|
|
type Text struct {
|
Menu Toolbar for Editor + Shell Prompts + Theme
* Added a "menu toolbar" to the top of the Edit Mode with useful buttons
that work: New Level, New Doodad (same thing), Save, Save as, Open.
* Added ability for the dev console to prompt the user for a question,
which opens the console automatically. "Save", "Save as" and "Load"
ask for their filenames this way.
* Started groundwork for theming the app. The palette window is a light
brown with an orange title bar, the Menu Toolbar has a black
background, etc.
* Added support for multiple fonts instead of just monospace. DejaVu
Sans (normal and bold) are used now for most labels and window titles,
respectively. The dev console uses DejaVu Sans Mono as before.
* Update ui.Label to accept PadX and PadY separately instead of only
having the Padding option which did both.
* Improvements to Frame packing algorithm.
* Set the SDL draw mode to BLEND so we can use alpha colors properly,
so now the dev console is semi-translucent.
2018-08-12 00:30:00 +00:00
|
|
|
Text string
|
|
|
|
Size int
|
|
|
|
Color Color
|
|
|
|
Padding int32
|
|
|
|
PadX int32
|
|
|
|
PadY int32
|
|
|
|
Stroke Color // Stroke color (if not zero)
|
|
|
|
Shadow Color // Drop shadow color (if not zero)
|
|
|
|
FontFilename string // Path to *.ttf file on disk
|
2018-07-22 00:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t Text) String() string {
|
2018-08-11 00:19:47 +00:00
|
|
|
return fmt.Sprintf(`Text<"%s" %dpx %s>`, t.Text, t.Size, t.Color)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsZero returns if the Text is the zero value.
|
|
|
|
func (t Text) IsZero() bool {
|
|
|
|
return t.Text == "" && t.Size == 0 && t.Color == Invisible && t.Padding == 0 && t.Stroke == Invisible && t.Shadow == Invisible
|
2018-07-22 00:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common color names.
|
|
|
|
var (
|
2018-07-25 03:57:22 +00:00
|
|
|
Invisible = Color{}
|
|
|
|
White = RGBA(255, 255, 255, 255)
|
|
|
|
Grey = RGBA(153, 153, 153, 255)
|
|
|
|
Black = RGBA(0, 0, 0, 255)
|
|
|
|
SkyBlue = RGBA(0, 153, 255, 255)
|
|
|
|
Blue = RGBA(0, 0, 255, 255)
|
|
|
|
DarkBlue = RGBA(0, 0, 153, 255)
|
|
|
|
Red = RGBA(255, 0, 0, 255)
|
|
|
|
DarkRed = RGBA(153, 0, 0, 255)
|
|
|
|
Green = RGBA(0, 255, 0, 255)
|
|
|
|
DarkGreen = RGBA(0, 153, 0, 255)
|
|
|
|
Cyan = RGBA(0, 255, 255, 255)
|
|
|
|
DarkCyan = RGBA(0, 153, 153, 255)
|
|
|
|
Yellow = RGBA(255, 255, 0, 255)
|
|
|
|
DarkYellow = RGBA(153, 153, 0, 255)
|
|
|
|
Magenta = RGBA(255, 0, 255, 255)
|
|
|
|
Purple = RGBA(153, 0, 153, 255)
|
|
|
|
Pink = RGBA(255, 153, 255, 255)
|
2018-07-22 00:12:22 +00:00
|
|
|
)
|
2018-07-24 03:10:53 +00:00
|
|
|
|
|
|
|
// IterLine is a generator that returns the X,Y coordinates to draw a line.
|
|
|
|
// https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)
|
|
|
|
func IterLine(x1, y1, x2, y2 int32) chan Point {
|
|
|
|
generator := make(chan Point)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
var (
|
|
|
|
dx = float64(x2 - x1)
|
|
|
|
dy = float64(y2 - y1)
|
|
|
|
)
|
|
|
|
var step float64
|
|
|
|
if math.Abs(dx) >= math.Abs(dy) {
|
|
|
|
step = math.Abs(dx)
|
|
|
|
} else {
|
|
|
|
step = math.Abs(dy)
|
|
|
|
}
|
|
|
|
|
|
|
|
dx = dx / step
|
|
|
|
dy = dy / step
|
|
|
|
x := float64(x1)
|
|
|
|
y := float64(y1)
|
|
|
|
for i := 0; i <= int(step); i++ {
|
|
|
|
generator <- Point{
|
|
|
|
X: int32(x),
|
|
|
|
Y: int32(y),
|
|
|
|
}
|
|
|
|
x += dx
|
|
|
|
y += dy
|
|
|
|
}
|
|
|
|
|
|
|
|
close(generator)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return generator
|
|
|
|
}
|
|
|
|
|
2018-07-25 00:44:32 +00:00
|
|
|
// IterLine2 works with two Points rather than four coordinates.
|
2018-07-24 03:10:53 +00:00
|
|
|
func IterLine2(p1 Point, p2 Point) chan Point {
|
|
|
|
return IterLine(p1.X, p1.Y, p2.X, p2.Y)
|
|
|
|
}
|