length
This commit is contained in:
34
line2d.cm
34
line2d.cm
@@ -20,7 +20,7 @@ var line_proto = {
|
||||
},
|
||||
|
||||
set_point: function(i, x, y) {
|
||||
if (i >= 0 && i < this.points.length) {
|
||||
if (i >= 0 && i < length(this.points)) {
|
||||
this.points[i].x = x
|
||||
this.points[i].y = y
|
||||
this._dirty = true
|
||||
@@ -58,7 +58,7 @@ var line_proto = {
|
||||
|
||||
function build_polyline_mesh(line) {
|
||||
var points = line.points || []
|
||||
if (points.length < 2) return {verts: [], indices: [], cumulative_lengths: [], total_length: 0}
|
||||
if (length(points) < 2) return {verts: [], indices: [], cumulative_lengths: [], total_length: 0}
|
||||
|
||||
var width = line.width || 10
|
||||
var widths = line.widths
|
||||
@@ -78,7 +78,7 @@ function build_polyline_mesh(line) {
|
||||
|
||||
// Transform points if in local space
|
||||
var pts = []
|
||||
for (var i = 0; i < points.length; i++) {
|
||||
for (var i = 0; i < length(points); i++) {
|
||||
var p = points[i]
|
||||
if (points_space == 'local') {
|
||||
pts.push({x: p.x + pos.x, y: p.y + pos.y})
|
||||
@@ -89,12 +89,12 @@ function build_polyline_mesh(line) {
|
||||
|
||||
// Calculate cumulative distances
|
||||
var cumulative = [0]
|
||||
for (var i = 1; i < pts.length; i++) {
|
||||
for (var i = 1; i < length(pts); i++) {
|
||||
var dx = pts[i].x - pts[i-1].x
|
||||
var dy = pts[i].y - pts[i-1].y
|
||||
cumulative.push(cumulative[i-1] + math.sqrt(dx*dx + dy*dy))
|
||||
}
|
||||
var total_length = cumulative[cumulative.length - 1]
|
||||
var total_length = cumulative[length(cumulative) - 1]
|
||||
|
||||
// Build triangle strip mesh
|
||||
var verts = []
|
||||
@@ -102,7 +102,7 @@ function build_polyline_mesh(line) {
|
||||
|
||||
// Get width at point i
|
||||
function get_width(i) {
|
||||
if (widths && widths.length > i) return widths[i]
|
||||
if (widths && length(widths) > i) return widths[i]
|
||||
return width
|
||||
}
|
||||
|
||||
@@ -123,10 +123,10 @@ function build_polyline_mesh(line) {
|
||||
|
||||
// Calculate normals at each point
|
||||
var normals = []
|
||||
for (var i = 0; i < pts.length; i++) {
|
||||
var prev = i > 0 ? pts[i-1] : (closed ? pts[pts.length-1] : null)
|
||||
for (var i = 0; i < length(pts); i++) {
|
||||
var prev = i > 0 ? pts[i-1] : (closed ? pts[length(pts)-1] : null)
|
||||
var curr = pts[i]
|
||||
var next = i < pts.length-1 ? pts[i+1] : (closed ? pts[0] : null)
|
||||
var next = i < length(pts)-1 ? pts[i+1] : (closed ? pts[0] : null)
|
||||
|
||||
var n = {x: 0, y: 0}
|
||||
|
||||
@@ -183,7 +183,7 @@ function build_polyline_mesh(line) {
|
||||
}
|
||||
|
||||
// Generate vertices (2 per point - left and right of line)
|
||||
for (var i = 0; i < pts.length; i++) {
|
||||
for (var i = 0; i < length(pts); i++) {
|
||||
var p = pts[i]
|
||||
var n = normals[i]
|
||||
var w = get_width(i) * 0.5
|
||||
@@ -209,7 +209,7 @@ function build_polyline_mesh(line) {
|
||||
}
|
||||
|
||||
// Generate indices (triangle strip as triangles)
|
||||
for (var i = 0; i < pts.length - 1; i++) {
|
||||
for (var i = 0; i < length(pts) - 1; i++) {
|
||||
var base = i * 2
|
||||
// First triangle
|
||||
indices.push(base + 0)
|
||||
@@ -222,8 +222,8 @@ function build_polyline_mesh(line) {
|
||||
}
|
||||
|
||||
// Handle closed path
|
||||
if (closed && pts.length > 2) {
|
||||
var last = (pts.length - 1) * 2
|
||||
if (closed && length(pts) > 2) {
|
||||
var last = (length(pts) - 1) * 2
|
||||
indices.push(last + 0)
|
||||
indices.push(last + 1)
|
||||
indices.push(0)
|
||||
@@ -235,10 +235,10 @@ function build_polyline_mesh(line) {
|
||||
// Add round caps if requested
|
||||
if (!closed && cap == 'round') {
|
||||
add_round_cap(verts, indices, pts[0], normals[0], get_width(0), get_u(0), v_offset, v_scale, true)
|
||||
add_round_cap(verts, indices, pts[pts.length-1], normals[pts.length-1], get_width(pts.length-1), get_u(pts.length-1), v_offset, v_scale, false)
|
||||
add_round_cap(verts, indices, pts[length(pts)-1], normals[length(pts)-1], get_width(length(pts)-1), get_u(length(pts)-1), v_offset, v_scale, false)
|
||||
} else if (!closed && cap == 'square') {
|
||||
add_square_cap(verts, indices, pts[0], normals[0], get_width(0), get_u(0), v_offset, v_scale, true, pts[1])
|
||||
add_square_cap(verts, indices, pts[pts.length-1], normals[pts.length-1], get_width(pts.length-1), get_u(pts.length-1), v_offset, v_scale, false, pts[pts.length-2])
|
||||
add_square_cap(verts, indices, pts[length(pts)-1], normals[length(pts)-1], get_width(length(pts)-1), get_u(length(pts)-1), v_offset, v_scale, false, pts[length(pts)-2])
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -252,7 +252,7 @@ function build_polyline_mesh(line) {
|
||||
function add_round_cap(verts, indices, p, n, width, u, v_offset, v_scale, is_start) {
|
||||
var w = width * 0.5
|
||||
var segments = 8
|
||||
var base_idx = verts.length
|
||||
var base_idx = length(verts)
|
||||
|
||||
// Direction along the line
|
||||
var dx = is_start ? -n.y : n.y
|
||||
@@ -293,7 +293,7 @@ function add_round_cap(verts, indices, p, n, width, u, v_offset, v_scale, is_sta
|
||||
|
||||
function add_square_cap(verts, indices, p, n, width, u, v_offset, v_scale, is_start, adjacent) {
|
||||
var w = width * 0.5
|
||||
var base_idx = verts.length
|
||||
var base_idx = length(verts)
|
||||
|
||||
// Direction along the line (away from adjacent point)
|
||||
var dx = p.x - adjacent.x
|
||||
|
||||
Reference in New Issue
Block a user