// rshapes draws shapes with random colors, openvg.Strokes, and sizes. func rshapes(width, height, n int) { var sx, sy, cx, cy, px, py, ex, ey, pox, poy float64 np := 10 polyx := make([]float64, np) polyy := make([]float64, np) openvg.Start(width, height) for i := 0; i < n; i++ { openvg.FillRGB(randcolor(), randcolor(), randcolor(), rand.Float64()) openvg.Ellipse(randf(width), randf(height), randf(200), randf(100)) openvg.Circle(randf(width), randf(height), randf(100)) openvg.Rect(randf(width), randf(height), randf(200), randf(100)) openvg.Arc(randf(width), randf(height), randf(200), randf(200), randf(360), randf(360)) sx = randf(width) sy = randf(height) openvg.StrokeRGB(randcolor(), randcolor(), randcolor(), 1) openvg.StrokeWidth(randf(5)) openvg.Line(sx, sy, sx+randf(200), sy+randf(100)) openvg.StrokeWidth(0) sx = randf(width) sy = randf(height) ex = sx + randf(200) ey = sy cx = sx + ((ex - sx) / 2.0) cy = sy + randf(100) openvg.Qbezier(sx, sy, cx, cy, ex, ey) sx = randf(width) sy = randf(height) ex = sx + randf(200) ey = sy cx = sx + ((ex - sx) / 2.0) cy = sy + randf(100) px = cx py = sy - randf(100) openvg.Cbezier(sx, sy, cx, cy, px, py, ex, ey) pox = randf(width) poy = randf(height) for j := 0; j < np; j++ { polyx[j] = pox + randf(200) polyy[j] = poy + randf(100) } openvg.Polygon(polyx, polyy) // , np) pox = randf(width) poy = randf(height) for j := 0; j < np; j++ { polyx[j] = pox + randf(200) polyy[j] = poy + randf(100) } openvg.Polyline(polyx, polyy) // , np) } openvg.FillRGB(128, 0, 0, 1) openvg.Text(20, 20, "OpenVG on the Raspberry Pi", "sans", 32) openvg.End() }
// drop shows the raindrop icon func (d *dimen) drop(color string) { x, y, w, h := d.x, d.y, d.width, d.height openvg.FillColor(color) openvg.Ellipse(x+(w/2), y+(h*0.40), w*0.52, h*0.65) xp := []openvg.VGfloat{x + (w / 2), x + (w * 0.25), x + (w * 0.75)} yp := []openvg.VGfloat{y + h, y + (h / 2), y + (h / 2)} openvg.Polygon(xp, yp) }
func arrowhand(cx, cy, px, py, r openvg.VGfloat, t float64, value int, color string) { ax := []openvg.VGfloat{cx, 0, px, 0, cx} ay := []openvg.VGfloat{cy, 0, py, 0, cy} t = minadjust(t, value) * deg2rad rf := float64(r * 0.9) tf := math.Pi / 45.0 ax[1] = cx + openvg.VGfloat(rf*math.Cos(t-tf)) ay[1] = cy + openvg.VGfloat(rf*math.Sin(t-tf)) ax[3] = cx + openvg.VGfloat(rf*math.Cos(t+tf)) ay[3] = cy + openvg.VGfloat(rf*math.Sin(t+tf)) openvg.FillColor(color) openvg.Polygon(ax, ay) }
// refcard shows a reference card of shapes func refcard(width, height int) { shapenames := []string{ "Circle", "Ellipse", "Rectangle", "Rounded Rectangle", "Line", "Polyline", "Polygon", "Arc", "Quadratic Bezier", "Cubic Bezier", "Image", } top := float64(height) * .95 sx := float64(width) * 0.10 sy := top sw := float64(width) * .05 sh := float64(height) * .045 dotsize := 7.0 spacing := 2.0 fontsize := int(float64(height) * .033) shapecolor := Color{202, 225, 255, 1.0} openvg.Start(width, height) openvg.FillRGB(128, 0, 0, 1) openvg.TextEnd(float64(width-20), float64(height/2), "OpenVG on the Raspberry Pi", "sans", fontsize+(fontsize/2)) openvg.FillRGB(0, 0, 0, 1) for _, s := range shapenames { openvg.Text(sx+sw+sw/2, sy, s, "sans", fontsize) sy -= sh * spacing } sy = top cx := sx + (sw / 2) ex := sx + sw openvg.FillRGB(shapecolor.red, shapecolor.green, shapecolor.blue, shapecolor.alpha) openvg.Circle(cx, sy, sw) coordpoint(cx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Ellipse(cx, sy, sw, sh) coordpoint(cx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Rect(sx, sy, sw, sh) coordpoint(sx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Roundrect(sx, sy, sw, sh, 20, 20) coordpoint(sx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.StrokeWidth(1) openvg.StrokeRGB(204, 204, 204, 1) openvg.Line(sx, sy, ex, sy) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(ex, sy, dotsize, shapecolor) sy -= sh px := []float64{sx, sx + (sw / 4), sx + (sw / 2), sx + ((sw * 3) / 4), sx + sw} py := []float64{sy, sy - sh, sy, sy - sh, sy} openvg.Polyline(px, py) // , 5) coordpoint(px[0], py[0], dotsize, shapecolor) coordpoint(px[1], py[1], dotsize, shapecolor) coordpoint(px[2], py[2], dotsize, shapecolor) coordpoint(px[3], py[3], dotsize, shapecolor) coordpoint(px[4], py[4], dotsize, shapecolor) sy -= sh * spacing py[0] = sy py[1] = sy - sh py[2] = sy - (sh / 2) py[3] = py[1] - (sh / 4) py[4] = sy openvg.Polygon(px, py) // , 5) sy -= (sh * spacing) + sh openvg.Arc(sx+(sw/2), sy, sw, sh, 0, 180) coordpoint(sx+(sw/2), sy, dotsize, shapecolor) sy -= sh * spacing var cy, ey float64 cy = sy + (sh / 2) ey = sy openvg.Qbezier(sx, sy, cx, cy, ex, ey) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(cx, cy, dotsize, shapecolor) coordpoint(ex, ey, dotsize, shapecolor) sy -= sh * spacing ey = sy cy = sy + sh openvg.Cbezier(sx, sy, cx, cy, cx, sy, ex, ey) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(cx, cy, dotsize, shapecolor) coordpoint(cx, sy, dotsize, shapecolor) coordpoint(ex, ey, dotsize, shapecolor) sy -= (sh * spacing * 1.5) openvg.Image(sx, sy, 110, 110, "starx.jpg") openvg.End() }
// plot places a plot at the specified location with the specified dimemsions // using the specified settings, using the specified data func plot(x, y, w, h float64, settings plotset, d []rawdata) { nd := len(d) if nd < 2 { fmt.Fprintf(os.Stderr, "%d is not enough points to plot\n", len(d)) return } // Compute the minima and maxima of the data maxx, minx := d[0].x, d[0].x maxy, miny := d[0].y, d[0].y for _, v := range d { if v.x > maxx { maxx = v.x } if v.y > maxy { maxy = v.y } if v.x < minx { minx = v.x } if v.y < miny { miny = v.y } } // Prepare for a area or line chart by allocating // polygon coordinates; for the horizon plot, you need two extra coordinates // for the extrema. needpoly := settings.opt["area"] || settings.opt["connect"] var xpoly, ypoly []float64 if needpoly { xpoly = make([]float64, nd+2) ypoly = make([]float64, nd+2) // preload the extrema of the polygon, // the bottom left and bottom right of the plot's rectangle xpoly[0] = x ypoly[0] = y xpoly[nd+1] = x + w ypoly[nd+1] = y } // Draw the plot's bounding rectangle if settings.opt["showbg"] && !settings.opt["sameplot"] { openvg.FillColor(settings.attr["bgcolor"]) openvg.Rect(x, y, w, h) } // Loop through the data, drawing items as specified spacer := 10.0 for i, v := range d { xp := fmap(v.x, minx, maxx, x, x+w) yp := fmap(v.y, miny, maxy, y, y+h) if needpoly { xpoly[i+1] = xp ypoly[i+1] = yp } if settings.opt["showbar"] { openvg.StrokeColor(settings.attr["barcolor"]) openvg.StrokeWidth(settings.size["barsize"]) openvg.Line(xp, yp, xp, y) } if settings.opt["showdot"] { openvg.FillColor(settings.attr["dotcolor"]) openvg.StrokeWidth(0) openvg.Circle(xp, yp, settings.size["dotsize"]) } if settings.opt["showx"] { if i%int(settings.size["xinterval"]) == 0 { openvg.FillColor("black") openvg.TextMid(xp, y-(spacer*2), fmt.Sprintf("%d", int(v.x)), settings.attr["font"], int(settings.size["fontsize"])) openvg.StrokeColor("silver") openvg.StrokeWidth(1) openvg.Line(xp, y, xp, y-spacer) } openvg.StrokeWidth(0) } } // Done constructing the points for the area or line plots, display them in one shot if settings.opt["area"] { openvg.FillColor(settings.attr["areacolor"]) openvg.Polygon(xpoly, ypoly) } if settings.opt["connect"] { openvg.StrokeColor(settings.attr["linecolor"]) openvg.StrokeWidth(settings.size["linesize"]) openvg.Polyline(xpoly[1:nd+1], ypoly[1:nd+1]) } // Put on the y axis labels, if specified if settings.opt["showy"] { bot := math.Floor(miny) top := math.Ceil(maxy) yrange := top - bot interval := yrange / float64(settings.size["yinterval"]) for yax := bot; yax <= top; yax += interval { yaxp := fmap(yax, bot, top, float64(y), float64(y+h)) openvg.FillColor("black") openvg.TextEnd(x-spacer, yaxp, fmt.Sprintf("%.1f", yax), settings.attr["font"], int(settings.size["fontsize"])) openvg.StrokeColor("silver") openvg.StrokeWidth(1) openvg.Line(x-spacer, yaxp, x, yaxp) } openvg.StrokeWidth(0) } // Finally, tack on the label, if specified if len(settings.attr["label"]) > 0 { openvg.FillColor(settings.attr["labelcolor"], 0.3) openvg.TextMid(x+(w/2), y+(h/2), settings.attr["label"], settings.attr["font"], int(w/8)) // int(settings.size["fontsize"])) } openvg.StrokeWidth(0) }