MCPcopy Index your code
hub / github.com/endbasic/endbasic / parse_polygon_coordinates_from_array

Function parse_polygon_coordinates_from_array

std/src/gfx/mod.rs:102–159  ·  view source on GitHub ↗

Parses an array reference that contains a variable-length sequence of X/Y coordinate pairs.

(scope: &Scope<'_>)

Source from the content-addressed store, hash-verified

100
101/// Parses an array reference that contains a variable-length sequence of X/Y coordinate pairs.
102fn parse_polygon_coordinates_from_array(scope: &Scope<'_>) -> CallResult<Vec<PixelsXY>> {
103 debug_assert_eq!(1, scope.nargs());
104
105 let array = scope.get_ref(0);
106 let pos = scope.get_pos(0);
107 if array.vtype != ExprType::Integer {
108 return Err(CallError::Syntax(pos, "Expected an INTEGER array".to_owned()));
109 }
110
111 let dimensions = array.array_dimensions();
112 match dimensions {
113 [ncoords] => {
114 if !ncoords.is_multiple_of(2) {
115 return Err(CallError::Syntax(
116 pos,
117 "Expected an even number of coordinates".to_owned(),
118 ));
119 }
120
121 let mut points = Vec::with_capacity(*ncoords / 2);
122 for i in (0..*ncoords).step_by(2) {
123 let x = array
124 .deref_array_integer(&[i as i32])
125 .map_err(|e| CallError::Syntax(pos, e))?;
126 let y = array
127 .deref_array_integer(&[(i + 1) as i32])
128 .map_err(|e| CallError::Syntax(pos, e))?;
129 points.push(PixelsXY {
130 x: parse_coordinate_value(pos, x)?,
131 y: parse_coordinate_value(pos, y)?,
132 });
133 }
134 Ok(points)
135 }
136
137 [npoints, 2] => {
138 let mut points = Vec::with_capacity(*npoints);
139 for i in 0..*npoints {
140 let x = array
141 .deref_array_integer(&[i as i32, 0])
142 .map_err(|e| CallError::Syntax(pos, e))?;
143 let y = array
144 .deref_array_integer(&[i as i32, 1])
145 .map_err(|e| CallError::Syntax(pos, e))?;
146 points.push(PixelsXY {
147 x: parse_coordinate_value(pos, x)?,
148 y: parse_coordinate_value(pos, y)?,
149 });
150 }
151 Ok(points)
152 }
153
154 _ => Err(CallError::Syntax(
155 pos,
156 "Expected a flat array of coordinates or an Nx2 array of points".to_owned(),
157 )),
158 }
159}

Callers 1

Calls 6

parse_coordinate_valueFunction · 0.85
get_refMethod · 0.80
get_posMethod · 0.80
array_dimensionsMethod · 0.80
deref_array_integerMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected