C++ 按变量循环的增量

C++ 按变量循环的增量,c++,for-loop,increment,C++,For Loop,Increment,我把声音作为一个叫做scaledVol的浮点数。我想更改scaledVol绘制的字母间距 这是代码片段: for (int i = 0; i < camWidth; i+=7){ for (int j = 0; j < camHeight; j+=9){ // get the pixel and its lightness (lightness is the average of its RGB values) float lightness

我把声音作为一个叫做scaledVol的浮点数。我想更改scaledVol绘制的字母间距

这是代码片段:

for (int i = 0; i < camWidth; i+=7){
    for (int j = 0; j < camHeight; j+=9){
        // get the pixel and its lightness (lightness is the average of its RGB values)
        float lightness = pixelsRef.getColor(i,j).getLightness();
        // calculate the index of the character from our asciiCharacters array
        int character = powf( ofMap(lightness, 0, 255, 0, 1), 2.5) * asciiCharacters.size();
        // draw the character at the correct location
        ofSetColor(0, 255, 0);
        font.drawString(ofToString(asciiCharacters[character]), f, f);
    }
}
for(int i=0;i
其中i设置字符间距之间的宽度,j设置字符间距之间的高度


我不想增加7或9,而是希望增加一个名为scaledVol的浮点


我不想增加7或9,而是希望增加一个名为scaledVol的浮点

然后编码:

 for (int i = 0; i < camWidth; i+=(int)scaledVol){

int incr=static_cast(地板(缩放体积));
甚至可能

int incr=reinterpret_cast(地板(缩放体积));
如果两种数字类型的大小相同,则可能无法正常工作


我不想增加7或9,而是希望增加一个名为scaledVol的浮点

然后编码:

 for (int i = 0; i < camWidth; i+=(int)scaledVol){

int incr=static_cast(地板(缩放体积));
甚至可能

int incr=reinterpret_cast(地板(缩放体积));
如果两种数字类型的大小相同,则可能无法正常工作

for (float i = 0.0f; i < camWidth; i+=scaleVol){
for(浮点i=0.0f;i
假设
camWidth
是一个浮点数。如果不是,则将其转换为浮点数

当将
scaledVol
转换为int

时,这也会解决舍入错误问题,需要类似

for (float i = 0.0f; i < camWidth; i+=scaleVol){
for(浮点i=0.0f;i
假设
camWidth
是一个浮点数。如果不是,则将其转换为浮点数


当将
scaledVol
转换为int

时,也会出现舍入错误问题。您可以使用
float
作为两个循环变量的类型,然后将它们转换为
int

for (float x = 0; (int)x < camWidth; x+=scaledVol) {
  int i = (int)x;
  for (float y = 0; (int)y < camHeight; y+=scaledVol) {
    int j = (int)y;
    // the rest of the code using i and j
  }
}
for(float x=0;(int)x

请注意,
scaledVol
最好大于1,否则,
i
j
的连续值将相等。您在`//代码其余部分``中的处理方式可能不同。

您可以使用
float
作为两个循环变量的类型,然后将它们转换为
int

for (float x = 0; (int)x < camWidth; x+=scaledVol) {
  int i = (int)x;
  for (float y = 0; (int)y < camHeight; y+=scaledVol) {
    int j = (int)y;
    // the rest of the code using i and j
  }
}
for(float x=0;(int)x

请注意,
scaledVol
最好大于1,否则,
i
j
的连续值将相等。您在`//代码其余部分``中的处理可能不喜欢这样。

“我希望通过称为scaledVol的浮点递增,而不是递增7或9”你试过用scaledVol来替换7或9吗?我试过用scaledVol来替换7或9,当xcode编译项目时,它不会运行“我想用scaledVol来递增7或9,而不是递增7或9”你试过用scaledVol替换7或9吗?我试过用scaledVol替换7或9,当xcode编译项目时,它不会运行希望
incr!=0
你有可能出现舍入错误(因此循环太频繁)我尝试了此操作,但它会编译,但调试应用程序没有响应。不幸的是,我没有看到任何错误。c样式转换是个坏主意。请使用
int(scaledVol)
而不是
(int)ScReDVOL 谢谢,设法使用它并使它工作。首先必须映射变量,然后C++ C++技巧的技术解决了我的问题。谢谢。希望<代码> in Curr.O.= 0 < /Cord>您有舍入错误的可能性(因此经常循环)我尝试了此操作,但它会编译,但调试应用程序没有响应。不幸的是,我没有看到任何错误。c样式转换是个坏主意。请使用
int(scaledVol)
而不是
(int)ScRead VoL<代码>谢谢,设法使用这个并使它工作。首先必须映射我的变量,但是你的C++友好的技术解决了我的问题。谢谢。
for (float x = 0; (int)x < camWidth; x+=scaledVol) {
  int i = (int)x;
  for (float y = 0; (int)y < camHeight; y+=scaledVol) {
    int j = (int)y;
    // the rest of the code using i and j
  }
}