Skip to content

Commit

Permalink
bounded rotate works, ish
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Charczuk committed Sep 5, 2016
1 parent 8f56e59 commit a1835a5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 27 deletions.
41 changes: 27 additions & 14 deletions _examples/text_rotation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
TextRotationDegrees: 45.0,
},
Ticks: []chart.Tick{
{0.0, "0.00"},
{2.0, "2.00"},
{4.0, "4.00"},
{6.0, "6.00"},
{8.0, "Eight"},
{10.0, "Ten"},
{Value: 0.0, Label: "0.00"},
{Value: 2.0, Label: "2.00"},
{Value: 4.0, Label: "4.00"},
{Value: 6.0, Label: "6.00"},
{Value: 8.0, Label: "Eight"},
{Value: 10.0, Label: "Ten"},
},
},
XAxis: chart.XAxis{
Expand All @@ -40,12 +40,12 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
TextRotationDegrees: 45.0,
},
Ticks: []chart.Tick{
{0.0, "0.00"},
{2.0, "2.00"},
{4.0, "4.00"},
{6.0, "6.00"},
{8.0, "Eight"},
{10.0, "Ten"},
{Value: 0.0, Label: "0.00"},
{Value: 2.0, Label: "2.00"},
{Value: 4.0, Label: "4.00"},
{Value: 6.0, Label: "6.00"},
{Value: 8.0, Label: "Eight"},
{Value: 10.0, Label: "Ten"},
},
},
Series: []chart.Series{
Expand All @@ -60,15 +60,28 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
graph.Elements = []chart.Renderable{
func(r chart.Renderer, cb chart.Box, defaults chart.Style) {

b := chart.Box{50, 50, 90, 110}
b := chart.Box{Top: 50, Left: 50, Right: 150, Bottom: 300}

cx, cy := b.Center()

chart.Draw.Box(r, chart.Box{Top: cy - 2, Left: cx - 2, Right: cx + 2, Bottom: cy + 2}, chart.Style{
StrokeWidth: 2,
StrokeColor: chart.ColorBlack,
})

chart.Draw.Box(r, b, chart.Style{
StrokeWidth: 2,
StrokeColor: chart.ColorBlue,
})
chart.Draw.Box(r, b.Rotate(chart.Math.DegreesToRadians(45)), chart.Style{
chart.Draw.Box(r, b.BoundedRotate(chart.Math.DegreesToRadians(45)), chart.Style{
StrokeWidth: 2,
StrokeColor: chart.ColorRed,
})

chart.Draw.BoxRotated(r, b, chart.Math.DegreesToRadians(45), chart.Style{
StrokeWidth: 2,
StrokeColor: chart.ColorOrange,
})
},
}
graph.Render(chart.PNG, res)
Expand Down
16 changes: 10 additions & 6 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,20 @@ func (b Box) OuterConstrain(bounds, other Box) Box {
return newBox
}

// Rotate rotates a box's corners by a given radian rotation angle.
func (b Box) Rotate(radians float64) Box {
// BoundedRotate rotates a box's corners by a given radian rotation angle
// and returns the maximum bounds or clipping rectangle.
func (b Box) BoundedRotate(radians float64) Box {
cx, cy := b.Center()

ltx, lty := Math.RotateCoordinate(cx, cy, b.Left, b.Top, radians)
lbx, lby := Math.RotateCoordinate(cx, cy, b.Left, b.Bottom, radians)
rtx, rty := Math.RotateCoordinate(cx, cy, b.Right, b.Top, radians)
rbx, rby := Math.RotateCoordinate(cx, cy, b.Right, b.Bottom, radians)

return Box{
Left: ltx,
Top: lty,
Right: rbx,
Bottom: rby,
Left: Math.MinInt(ltx, lbx),
Top: Math.MinInt(lty, rty),
Right: Math.MaxInt(rtx, rbx),
Bottom: Math.MaxInt(lby, rby),
}
}
8 changes: 4 additions & 4 deletions box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestBoxCenter(t *testing.T) {
assert.Equal(20, cy)
}

func TestBoxRotate(t *testing.T) {
func TestBoxBoundedRotate(t *testing.T) {
assert := assert.New(t)

b := Box{
Expand All @@ -167,9 +167,9 @@ func TestBoxRotate(t *testing.T) {
Right: 20,
Bottom: 10,
}
rotated := b.Rotate(Math.DegreesToRadians(45))
rotated := b.BoundedRotate(Math.DegreesToRadians(45))
assert.Equal(1, rotated.Top)
assert.Equal(4, rotated.Left)
assert.Equal(10, rotated.Right)
assert.Equal(5, rotated.Left)
assert.Equal(19, rotated.Right)
assert.Equal(14, rotated.Bottom)
}
16 changes: 16 additions & 0 deletions draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,22 @@ func (d draw) Box(r Renderer, b Box, s Style) {
r.FillStroke()
}

func (d draw) BoxRotated(r Renderer, b Box, thetaRadians float64, s Style) {
s.WriteToRenderer(r)
cx, cy := b.Center()
ltx, lty := Math.RotateCoordinate(cx, cy, b.Left, b.Top, thetaRadians)
lbx, lby := Math.RotateCoordinate(cx, cy, b.Left, b.Bottom, thetaRadians)
rtx, rty := Math.RotateCoordinate(cx, cy, b.Right, b.Top, thetaRadians)
rbx, rby := Math.RotateCoordinate(cx, cy, b.Right, b.Bottom, thetaRadians)

r.MoveTo(ltx, lty)
r.LineTo(rtx, rty)
r.LineTo(rbx, rby)
r.LineTo(lbx, lby)
r.Close()
r.FillStroke()
}

// DrawText draws text with a given style.
func (d draw) Text(r Renderer, text string, x, y int, style Style) {
style.GetTextOptions().WriteToRenderer(r)
Expand Down
2 changes: 1 addition & 1 deletion math.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (m mathUtil) RotateCoordinate(cx, cy, x, y int, thetaRadians float64) (rx,
tempX, tempY := float64(x-cx), float64(y-cy)
rotatedX := tempX*math.Cos(thetaRadians) - tempY*math.Sin(thetaRadians)
rotatedY := tempX*math.Sin(thetaRadians) + tempY*math.Cos(thetaRadians)
rx = int(rotatedX) + cy
rx = int(rotatedX) + cx
ry = int(rotatedY) + cy
return
}
2 changes: 1 addition & 1 deletion raster_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (rr *rasterRenderer) MeasureText(body string) Box {
return textBox
}

return textBox.Rotate(*rr.rotateRadians)
return textBox.BoundedRotate(*rr.rotateRadians)
}

// SetTextRotation sets a text rotation.
Expand Down
2 changes: 1 addition & 1 deletion vector_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (vr *vectorRenderer) MeasureText(body string) (box Box) {
if vr.c.textTheta == nil {
return
}
box = box.Rotate(*vr.c.textTheta)
box = box.BoundedRotate(*vr.c.textTheta)
}
return
}
Expand Down

0 comments on commit a1835a5

Please sign in to comment.