The following is processing code for creating a square from 40*40 smaller squares. Did I…
- User
The following is processing code for creating a square from 40*40 smaller squares. Did I interpolate the u and v values correctly? for (int i = 0; i < 40; i++) { for (int j = 0; j < 40; j++) { float start_x = -100; float start_y = -100; start_x = start_x + i * 5; start_y = start_y + j * 5; float start_u = i / 40; float start_v = j / 40; float fourty = 1 / 40; beginShape(); vertex(start_x, start_y, 0, start_u, start_v); vertex(start_x + 5, start_y, 0, start_u + fourty, start_v); vertex(start_x + 5, start_y + 5, 0, start_u + fourty, start_v + fourty); vertex(start_x, start_y + 5, 0, start_u, start_v + fourty); endShape(); } } popMatrix();
- Assistant
The interpolation of the u and v values in the code seems to be incorrect.
In the code, the expression
float start_u = i / 40;andfloat start_v = j / 40;are using integer division, which will result in an integer quotient. To get the correct interpolation, you need to convert one or both of the operands to float before division.To fix this, you can change the expressions to
float start_u = (float)i / 40;andfloat start_v = (float)j / 40;to perform floating-point division.Here is the corrected code:
for (int i = 0; i < 40; i++) { for (int j = 0; j < 40; j++) { float start_x = -100; float start_y = -100; start_x = start_x + i * 5; start_y = start_y + j * 5; float start_u = (float)i / 40; float start_v = (float)j / 40; float forty = 1 / 40.0; beginShape(); vertex(start_x, start_y, 0, start_u, start_v); vertex(start_x + 5, start_y, 0, start_u + forty, start_v); vertex(start_x + 5, start_y + 5, 0, start_u + forty, start_v + forty); vertex(start_x, start_y + 5, 0, start_u, start_v + forty); endShape(); } } popMatrix();Note: I also corrected a variable name from
fourtytoforty.