Add exp/shape package

This commit is contained in:
Hajime Hoshi 2015-01-18 01:26:57 +09:00
parent c892f84816
commit db22bf6242
2 changed files with 95 additions and 0 deletions

View File

@ -16,8 +16,10 @@ package main
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/exp/shape"
"image/color"
"log"
"math"
)
const (
@ -33,6 +35,9 @@ func update(screen *ebiten.Image) error {
screen.FillRect(20, 20, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80})
screen.DrawLine(130, 0, 140, 100, color.NRGBA{0xff, 0x80, 0x80, 0x80})
screen.DrawLine(140, 0, 150, 100, color.NRGBA{0xff, 0x80, 0x80, 0x80})
shape.DrawEllipse(screen, 0, 0, 100, 50, color.NRGBA{0x80, 0xff, 0x80, 0x80})
shape.DrawArc(screen, 0, 0, 100, 50, 0, math.Pi/2, color.NRGBA{0xff, 0x80, 0x80, 0x80})
return nil
}

90
exp/shape/ellipse.go Normal file
View File

@ -0,0 +1,90 @@
// Copyright 2015 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shape
import (
"github.com/hajimehoshi/ebiten"
"image/color"
"math"
)
func DrawEllipse(s *ebiten.Image, x, y, width, height int, clr color.Color) error {
return s.DrawLines(&ellipsesLines{&rect{x, y, width, height, clr}, 0, 2 * math.Pi})
}
func DrawEllipses(s *ebiten.Image, rects ebiten.Rects) error {
return s.DrawLines(&ellipsesLines{rects, 0, 2 * math.Pi})
}
func DrawArc(s *ebiten.Image, x, y, width, height int, angle0, angle1 float64, clr color.Color) error {
return s.DrawLines(&ellipsesLines{&rect{x, y, width, height, clr}, angle0, angle1})
}
type ellipsesLines struct {
ebiten.Rects
angle0, angle1 float64
}
func (e *ellipsesLines) lineNum() int {
return 64
}
func (e *ellipsesLines) Len() int {
return e.Rects.Len() * e.lineNum()
}
func (e *ellipsesLines) Points(i int) (x0, y0, x1, y1 int) {
n := e.lineNum()
x, y, w, h := e.Rects.Rect(i / n)
part := float64(i % n)
theta0 := 2 * math.Pi * part / float64(n)
theta1 := 2 * math.Pi * (part + 1) / float64(n)
if theta0 < e.angle0 || e.angle1 < theta1 {
return 0, 0, 0, 0
}
theta0 = math.Max(theta0, e.angle0)
theta1 = math.Min(theta1, e.angle1)
fy0, fx0 := math.Sincos(theta0)
fy1, fx1 := math.Sincos(theta1)
hw, hh := (float64(w)-1)/2, (float64(h)-1)/2
fx0 = fx0*hw + float64(x) + hw
fx1 = fx1*hw + float64(x) + hw
fy0 = fy0*hh + float64(y) + hh
fy1 = fy1*hh + float64(y) + hh
return int(fx0), int(fy0), int(fx1), int(fy1)
}
func (e *ellipsesLines) Color(i int) color.Color {
return e.Rects.Color(i / e.lineNum())
}
// TODO: This is same as ebiten.rect.
type rect struct {
x, y int
width, height int
color color.Color
}
func (r *rect) Len() int {
return 1
}
func (r *rect) Rect(i int) (x, y, width, height int) {
return r.x, r.y, r.width, r.height
}
func (r *rect) Color(i int) color.Color {
return r.color
}