Skip to content

Commit

Permalink
Changed the way circles are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
eternalfrustation committed Oct 18, 2021
1 parent e6c8874 commit 4a02e86
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
3 changes: 2 additions & 1 deletion bvg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bvg

import (
"bytes"
"math"
"io"
"fmt"
"os"
Expand All @@ -25,7 +26,7 @@ func TestBvg(t *testing.T) {
}
t.Log("Added Triangles" + fmt.Sprintf(" bytes Encoded: %d", bytesEncoded))
for i := -1.0; i < 0.998; i += 0.1 {
in.Circles = append(in.Circles, NewCircleGrad(center, NewPtCol(i, 0.6, 232, 54, 87, 255)))
in.Circles = append(in.Circles, NewCircle(center.RelPt(i, math.Pi*i), 0.5, 0.999))
bytesEncoded += 41
}
t.Log("Added Circles" + fmt.Sprintf(" bytes Encoded: %d", bytesEncoded))
Expand Down
8 changes: 5 additions & 3 deletions dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ func Decode(b []byte) (bvg *Bvg , err error) {
))
i += n + 1
case byte('c'):
Pts, n := PointsFromBytes(b[i+1:i+42], 2)
bvg.Circles = append(bvg.Circles, NewCircleGrad(Pts[0], Pts[1]))
i += n + 1
Pts, n := PointsFromBytes(b[i+1:i+21], 1)
t1 := Float64FromBytes(b[i+21:i+29])
t2 := Float64FromBytes(b[i+29:i+37])
bvg.Circles = append(bvg.Circles, NewCircle(Pts[0], t1, t2))
i += n + 1 + 16
case byte('p'):
n := binary.LittleEndian.Uint32(b[i+1 : i+5])
// fmt.Println(n)
Expand Down
37 changes: 17 additions & 20 deletions enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bvg

import (
"encoding/binary"
// "fmt"
// "fmt"
// "fmt"
"io"
"math"
Expand Down Expand Up @@ -37,7 +37,6 @@ func NewPt(x, y float64) *Point {
}
}


func (p *Point) Dist(p1 *Point) float64 {
return math.Sqrt((p.X-p1.X)*(p.X-p1.X) + (p.Y-p1.Y)*(p.Y-p1.Y))
}
Expand Down Expand Up @@ -87,21 +86,19 @@ func NewLine(p1, p2 *Point) *Line {
}

type Circle struct {
P1 *Point
P2 *Point
P *Point
// r is the complete radius of the circle
// the alpha at r is 0
// t is threshold upto which the color of the circle
// does not fade
r, t float64
}

func NewCircle(p1 *Point, r float64) *Circle {
func NewCircle(p *Point, r, t float64) *Circle {
return &Circle{
P1: p1,
P2: p1.RelPt(r, 0),
}
}

func NewCircleGrad(p1, p2 *Point) *Circle {
return &Circle{
P1: p1,
P2: p2,
P: p,
r: r,
t: t,
}
}

Expand Down Expand Up @@ -182,7 +179,7 @@ func (b *Bvg) DrawLine(l Line) {

// draw a triangle from p1, p2, p3
func (b *Bvg) DrawTriangle(t Triangle) {
binary.Write(b.Writer, binary.LittleEndian, int8('t'))
binary.Write(b.Writer, binary.LittleEndian, int8('t'))
t.P1.Write(b.Writer)
t.P2.Write(b.Writer)
t.P3.Write(b.Writer)
Expand All @@ -193,17 +190,17 @@ func (b *Bvg) DrawTriangle(t Triangle) {
// the center to colours of p2 at the circumference
func (b *Bvg) DrawCircle(c Circle) {
binary.Write(b.Writer, binary.LittleEndian, int8('c'))
c.P1.Write(b.Writer)
c.P2.Write(b.Writer)
c.P.Write(b.Writer)
binary.Write(b.Writer, binary.LittleEndian, c.r)
binary.Write(b.Writer, binary.LittleEndian, c.t)
}


// draws a polygon from the points given, it triangulates by
// using a triangle strip from the given points
func (b *Bvg) DrawPoly(p Poly) {
binary.Write(b.Writer, binary.LittleEndian, int8('p'))
binary.Write(b.Writer, binary.LittleEndian, uint32(len(p.Pts)))
// fmt.Println(len(p.Pts))
// fmt.Println(len(p.Pts))
for _, p := range p.Pts {
p.Write(b.Writer)
}
Expand All @@ -226,6 +223,6 @@ func (b *Bvg) DrawLineStrip(l LineStrip) {
binary.Write(b.Writer, binary.LittleEndian, uint32(len(l.Pts)))
for _, p := range l.Pts {
p.Write(b.Writer)

}
}

0 comments on commit 4a02e86

Please sign in to comment.