我需要编写和算法用水平等距线填充闭合多义线.
我用矩形和圆圈做了类似的事情,这里是后者的代码片段:
// circle parameters: center(point(0).x, point(0).y), radius
int offsetX = point(0).x + radius;
int offsetY = point(0).y + radius;
for(int i = -radius; i < radius; i += spacing){
int ry = i;
int rx = sqrt(double(radius*radius - ry*ry));
// the parameters are pair of coordinates of the horizontal line
fl_line(offsetX - rx, offsetY + i,
offsetX + rx, offsetY + i);
}
Run Code Online (Sandbox Code Playgroud)
在闭合多义线的情况下,额外的难度(对我而言)是水平线的坐标不会从单个方程(圆,矩形的高度等)中提取,而是从方程中提取.具有相同"y"坐标的线,不会连续匹配.
这是感兴趣的函数的函数处理程序:
fun = @(x) 1 / (sqrt(x) * (x + 1));
q = integral(fun, 0, inf)
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Error using *
Inner matrix dimensions must agree.
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
我在这里有点困惑.我们来看看以下代码:
bool testing(int i) {
if((i%2)==0) {
return true;
} else {
--i;
testing(i);
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时testing(5)
,我希望函数返回true,因为在某些时候,函数将会5
变为4
,所以4 % 2 == 0
函数将返回,true
但事实并非如此.怎么了?
例如,如果输入是x+=5
,程序应返回的数组x
,+=
,5
.请注意,在x
和之间没有空格+=
,因此仅按空格分割可能不起作用,因为那样您将不得不重复遍历它以查找关键字.
我该怎么办?有没有一种有效的方法在C中执行此操作?