#Parsing SVG line into scatterplot coordinates.

8 messages · Page 1 of 1 (latest)

agile obsidian
#

That is intended to parse the bezier curves from an SVG file into a struct "CompositeBezierCurve" which is then plotted with:

// DeCasteljau computes a point on the Bezier curve at a given t
func (b BezierCurve) DeCasteljau(t float64) Point {
    n := len(b.ControlPoints) - 1
    tmp := make([]Point, n+1)
    copy(tmp, b.ControlPoints)
    for i := 1; i <= n; i++ {
        for j := 0; j <= n-i; j++ {
            tmp[j].X = (1-t)*tmp[j].X + t*tmp[j+1].X
            tmp[j].Y = (1-t)*tmp[j].Y + t*tmp[j+1].Y
        }
    }
    return tmp[0]
}

// DrawCompositeBezierCurve to draw a composite bezier curve
func (c CompositeBezierCurve) DrawCompositeBezierCurve() {
    // point XY coordinate storage
    pts := make(plotter.XYs, 0, 1000)
    // calculate points
    fmt.Println("Composite Bezier Curve")
    for t := 0.0; t <= 1; t += 0.1 {
        x, y := 0.0, 0.0
        for i, curve := range c.Curves {
            p := curve.DeCasteljau(t)

            x += p.X
            y += p.Y
            pts = append(pts, plotter.XY{X: x, Y: y})

            fmt.Printf("t=%f, curve=%d, (x=%f, y=%f)\n", t, i+1, p.X, p.Y)
        }
        fmt.Printf("t=%f, composite (x=%f, y=%f)\n", t, x, y)
    }
    // create a scatter plot
    p := plot.New()
    p.Title.Text = "Composite Bezier Curve"
    p.X.Label.Text = "X"
    p.Y.Label.Text = "Y"

    // make a scatter plotter and set its style.
    s, err := plotter.NewScatter(pts)
    if err != nil {
        panic(err)
    }
    s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255}
    p.Add(s)
    p.Save(10*vg.Inch, 10*vg.Inch, "points.png")
}

func main() {

    compositeCurve, err := GetSVGCoords("drawing.svg")
    if err != nil {
        fmt.Println(err)
        return
    }
    compositeCurve.DrawCompositeBezierCurve()
}
```**^^ I think all of this part works^^**
#

The problem is that it gets some of the points correctly but not all of them.

My curve looks like

#

But my plot looks like this

#

Somehow stuff ended up overlapped

#

I've spent so much time on this at this point and can't figure this out

agile obsidian
#

bump

agile obsidian
#

So I changed a lot of stuff... Still not getting the expected result.