Ask Your Question
0

Changing line colors randomly.

asked 2014-07-14 10:41:19 -0600

brycemh gravatar image

updated 2015-01-14 00:51:46 -0600

Hello all, I wrote a code that draws a line for every nth degree. I need to know how to create another loop that will change the color of every line. I have researched RNG and things of that nature, but nothing is working for me. Please help or give me tips if you can; below is my code.

    int x = shape->width;
    int y = shape->height;
    int m = 0;
    int l = 2;
    int q = 200;
    int z = shape->width / l;
    int a = shape->height / l;
    int b = 10;
    int f = -255;
    int g = 255;
    int w = 450;
    int v = 36;
    int h = 100;
    int p = l++;
    int r = 10;
    int s = r++;
    int t = 30;
    int u = t++;
    int c = 150;


    cvRectangle(shape,cvPoint(x,m),cvPoint(m,y),CV_RGB(m,g,m),f,g);

    //Drawing a Circle
    cvCircle(shape,cvPoint(z,a),q, CV_RGB(m,m,g),f);

    // for loop execution 
    for(int o = c; o < 511; o+=v){ 
    for(int n = c; n < 467; n+=v){ 
        cvLine(shape, cvPoint(z,a), cvPoint(o,n), CV_RGB(p,r,u),1,1);   
        }
    }

    cvCircle(shape,cvPoint(z,a),b,CV_RGB(m,g,m),f);
    cvCircle(shape,cvPoint(z,a),q, CV_RGB(g,m,m),h,g);
    //Showing the image
    cvShowImage("Shapes",shape);
    //Escape Sequence
    cvWaitKey(0);
    //CleanUp
    cvReleaseImage(&shape);
    cvDestroyAllWindows();

}
edit retag flag offensive close merge delete

Comments

3

just please stop using the arcane c-api. that's not allowed for noobs !

also: RNG r(getTickCount()); int randomNumber=r.uniform(0,256);

berak gravatar imageberak ( 2014-07-14 10:53:42 -0600 )edit

I do not see the random number generation in code. Also, it may be a good idea to move to the C++ interface.

unxnut gravatar imageunxnut ( 2014-07-14 10:54:06 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-01-13 12:02:28 -0600

bvbdort gravatar image

updated 2015-01-13 15:30:19 -0600

You can do it by generating random color using rand() function in c++

    for ( ) // inside loop
    {
       // create random color variable
       Scalar clr(rand() % 255,rand() % 255,rand() % 255);  

       // draw line with random color            
       cv::Line(shape, cv::Point(z,a), cv::Point(o,n), clr,1,1); 
    }
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-07-14 10:41:19 -0600

Seen: 6,731 times

Last updated: Jan 13 '15