I'm working on a project for school that's due soon and I've run into a pretty big issue with sorting my some tables in my program.
Some context first: The main portion of this simple program is working to make the slope of a line between 2 points, and then delete all values that are not towards the "target". For example I have my first x and y coordinate as (12,12), and my second as (45,45): so I need to delete all x values that are greater than 45, because they go away from my "target" so to speak.
So here's my code that's supposed remove all x values away from the target:
if greater_positive_bool == true then
for xer in pairs(x_list) do
if bull_coords_x < xer then
table.remove(x_list, xer)
end
end
end
The boolean variable at the top of the if statement is just to help decide which values should be deleted. One more thing, the list has 1001 total values (-500 to 500). So when tried to remove all values greater than 45, this is what happened:
table.getn(x_list)
=> 523
I knew something was up, but it get even stranger; re-iterating through the list returned values ranging from -500 to 500, like nothing was ever removed, even though the list length is only 523! I'm only in highschool so I've definitely made some stupid mistakes but this is by far the strangest thing I've encountered so far. Please help!
The slope function I came up with: (This part works just fine BTW)
function slope(x1, x2, y1, y2) --Slope formula
y_slp = (y2 - y1)
x_slp = (x2 - x1)
slp = (y_slp / x_slp)
b = psbl_y - (slp * (psbl_x)) --Solving for the y-intercept given x and y coords
print(slp)
run_x = {}
run_x_index = 0
y_list = {}
for i=_grid_x, grid_x do -- _grid_x = -500 and grid_x = 500
table.insert(run_x, i)
end
for x_value in pairs(run_x) do
run_x_index = run_x_index + 1
y_value = (slp * run_x[run_x_index]) + b
table.insert(y_list, y_value)
end
x_list = run_x
end