我正在尝试编写一个python函数,它将从图片上的任何位置复制三角形区域到新的空白图片.我可以将图片中的矩形区域复制到新的空白图片,但我不知道如何复制三角形.这就是我所拥有的,但它只复制一个矩形区域.对不起,如果它看起来很乱或过于复杂,但我刚刚开始如何用python编写.
def copyTriangle():
file=pickAFile()
oldPic=makePicture(file)
newPic=makeEmptyPicture(getWidth(oldPic),getHeight(oldPic))
xstart=getWidth(oldPic)/2
ystart=getHeight(oldPic)/2
for y in range(ystart,getHeight(oldPic)):
for x in range(xstart,getWidth(oldPic)):
oldPixel=getPixel(oldPic,x,y)
colour=getColor(oldPixel)
newPixel=getPixel(newPic,x,y)
setColor(newPixel,colour)
Run Code Online (Sandbox Code Playgroud) 我有一个数字列表,我想要添加数字,但我不想添加列表中的所有数字,只是添加选定的数字,如前三个.
list = [2, 3, 7, 11, 15, 21]
for i in list:
sum += i
Run Code Online (Sandbox Code Playgroud)
我的代码显然将列表中的所有数字相加.我已经尝试将for循环更改为范围(0,4),但这只是将数字0,1,2,3加在一起而不是我列表中的数字.那么如何修改我的代码以从我的列表中添加前三个数字.
我写了一个简单的嵌套For循环,但由于某种原因它永远不会停止.
#include <stdio.h>
int main (void) {
int x,y;
for (x=10;x<100;x+=10) {
for (y=10;y<100;x+=10)
printf("x is %d \n",x);
printf("y is %d \n",y);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我是c的新手,但是从我看过的例子来看,它应该在x和y达到100时停止.但由于某种原因,它会一直持续下去.