lib/rubyvis/scene/svg_line.rb in rubyvis-0.3.6 vs lib/rubyvis/scene/svg_line.rb in rubyvis-0.4.0

- old
+ new

@@ -13,11 +13,11 @@ stroke = s.stroke_style return e if (fill.opacity==0.0 and stroke.opacity==0.0) #/* points */ - d = "M" + s.left.to_s + "," + s.top.to_s + d = "M#{s.left},#{s.top}" if (scenes.size > 2 and (['basis', 'cardinal', 'monotone'].include? s.interpolate)) case (s.interpolate) when "basis" d = d+ curve_basis(scenes) @@ -53,41 +53,44 @@ e=scenes._g.elements[1] s = scenes[0]; paths=nil case s.interpolate - when "basis" - paths = curve_basis_segments(scenes) - when "cardinal" - paths=curve_cardinal_segments(scenes, s.tension) - when "monotone" - paths = curve_monotone_segments(scenes) + when "basis" + paths = curve_basis_segments(scenes) + when "cardinal" + paths=curve_cardinal_segments(scenes, s.tension) + when "monotone" + paths = curve_monotone_segments(scenes) end - (scenes.length-1).times {|i| + (0...(scenes.size-1)).each {|i| s1 = scenes[i] s2 = scenes[i + 1]; - + #p "#{s1.top} #{s1.left} #{s1.line_width} #{s1.interpolate} #{s1.line_join}" # visible - next if (!s1.visible and !s2.visible) + next if (!s1.visible or !s2.visible) stroke = s1.stroke_style - fill = Rubyvis.Color.transparent + fill = Rubyvis::Color.transparent next if stroke.opacity==0.0 # interpolate d=nil - if ((s1.interpolate == "linear") and (s1.lineJoin == "miter")) - fill = stroke; - stroke = Rubyvis.Color.transparent; - d = path_join(scenes[i - 1], s1, s2, scenes[i + 2]); + if ((s1.interpolate == "linear") and (s1.line_join == "miter")) + fill = stroke + stroke = Rubyvis::Color.transparent + s0=((i-1) < 0) ? nil : scenes[i-1] + s3=((i+2) >= scenes.size) ? nil : scenes[i+2] + + d = path_join(s0, s1, s2, s3) elsif(paths) d = paths[i] else - d = "M" + s1.left + "," + s1.top + path_segment(s1, s2); + d = "M#{s1.left},#{s1.top}#{path_segment(s1, s2)}" end e = SvgScene.expect(e, "path", { "shape-rendering"=> s1.antialias ? nil : "crispEdges", "pointer-events"=> s1.events, @@ -100,11 +103,11 @@ "stroke-width"=> stroke.opacity>0 ? s1.line_width / self.scale : nil, "stroke-linejoin"=> s1.line_join }); e = SvgScene.append(e, scenes, i); } - return e + e end # Returns the path segment for the specified points. */ def self.path_segment(s1, s2) @@ -146,40 +149,40 @@ p1 = Rubyvis.vector(s1.left, s1.top) p2 = Rubyvis.vector(s2.left, s2.top) - p = p2.minus(p1) + _p = p2.minus(p1) - v = p.perp().norm() + v = _p.perp().norm() + + w = v.times(s1.line_width / (2.0 * self.scale)) - w = v.times(s1.lineWidth / (2 * this.scale)) - a = p1.plus(w) b = p2.plus(w) c = p2.minus(w) d = p1.minus(w) - #/* # * Start join. P0 is the previous line segment's start point. We define the # * cutting plane as the average of the vector perpendicular to P0-P1, and # * the vector perpendicular to P1-P2. This insures that the cross-section of # * the line on the cutting plane is equal if the line-width is unchanged. # * Note that we don't implement miter limits, so these can get wild. # */ if (s0 and s0.visible) - v1 = p1.minus(s0.left, s0.top).perp().norm().plus(v); - d = line_intersect(p1, v1, d, p); - a = line_intersect(p1, v1, a, p); + v1 = p1.minus(s0.left, s0.top).perp().norm().plus(v) + d = line_intersect(p1, v1, d, _p) + a = line_intersect(p1, v1, a, _p) end #/* Similarly, for end join. */ - if (s3 && s3.visible) + if (s3 and s3.visible) v2 = Rubyvis.vector(s3.left, s3.top).minus(p2).perp().norm().plus(v); - c = line_intersect(p2, v2, c, p); - b = line_intersect(p2, v2, b, p); + c = line_intersect(p2, v2, c, _p); + b = line_intersect(p2, v2, b, _p); end - return "M" + a.x + "," + a.y+ "L" + b.x + "," + b.y+ " " + c.x + "," + c.y+ " " + d.x + "," + d.y + d="M#{a.x},#{a.y}L#{b.x},#{b.y} #{c.x},#{c.y} #{d.x},#{d.y}" + d end end end