Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // add these two include files...
- #include <cv.h>
- #include <highgui.h>
- #define W 900
- #define H 720
- void draw_line(IplImage *img2, int x1, int y1, int x2, int y2)
- {
- int xtemp,ytemp ;
- if(x1 > x2)
- {
- xtemp = x1 ; x1 = x2 ; x2 = xtemp ;
- ytemp = y1 ; y1 = y2 ; y2 = ytemp ;
- }
- //To fix the bug that point can't be printed from right-side to left-side
- if(x1 == x2)
- {
- cvLine(
- img2, //CvArr* img,
- cvPoint((x1 + W/2), (-y1 + H/2)), //CvPoint pt1,
- cvPoint((x1 + W/2), (-y2 + H/2)), //CvPoint pt2,
- cvScalar(0, 255, 0, 0), //CvScalar color,
- 1, 8, 0
- ); //int thickness=1, int line_type=8, int shift=0 );
- }
- else
- {
- int dx = x2-x1;
- int dy = y2-y1;
- float m = (float)dy/dx;
- float x, y; float IX, IY;
- for (x = x1; x < x2; x = x + 0.01)
- {
- //y = m*x + y1 + 0.5; //Plus 0.5 to provide for m wasn't an integer.
- y = m * (x - x1) + y1 ;
- IX = x + W/2;
- IY = -y + H/2;
- //putpixel(x, y);
- cvLine(
- img2, //CvArr* img,
- cvPoint(IX, IY), //CvPoint pt1,
- cvPoint(IX, IY), //CvPoint pt2,
- cvScalar(0, 255, 0, 0), //CvScalar color,
- 1, 8, 0
- ); //int thickness=1, int line_type=8, int shift=0 );
- }
- }
- }
- int main(int argc, char *argv[])
- {
- IplImage *img, *img2; // Just like FILE *fptr ...
- img2 = cvCreateImage(cvSize(W, H), //CvSize size,
- IPL_DEPTH_8U , //int depth,
- 3); //int channels
- printf("Channels %d depth %d \n", img2->nChannels, img2->depth);
- printf("The image Width by Height is %dX%d \n",
- img2->width,img2->height);
- printf("The image WidthStep %d \n", img2->widthStep);
- cvLine(
- img2, //CvArr* img,
- cvPoint(0, H/2), //CvPoint pt1,
- cvPoint(W-1, H/2), //CvPoint pt2,
- cvScalar(0, 0, 255, 0), //CvScalar color,
- 1, 8, 0); //int thickness=1, int line_type=8, int shift=0 );
- cvLine(
- img2, //CvArr* img,
- cvPoint(W/2, 0), //CvPoint pt1,
- cvPoint(W/2, H-1), //CvPoint pt2,
- cvScalar(0, 0, 255, 0), //CvScalar color,
- 1, 8, 0); //int thickness=1, int line_type=8, int shift=0 );
- /* I */
- draw_line(img2, 200, 200, 100, 100);
- /* II */
- draw_line(img2,-200, 200,-100, 100);
- /* III */
- draw_line(img2,-200,-200,-100,-100);
- /* IIII */
- draw_line(img2, 200,-200, 100,-100);
- /* horizontal */
- draw_line(img2,-200,-200, 200,-200);
- /* vertical */
- draw_line(img2,-100,-100,-100, 100);
- draw_line(img2, 100, 100, 100,-100);
- /* large slope */
- draw_line(img2, -20,-300, 20, 300);
- draw_line(img2, 20,-300, -20, 300);
- cvNamedWindow( "Fig2", 1 );
- cvShowImage("Fig2", img2 );
- cvWaitKey(0);
- cvDestroyWindow( "Fig2" );
- cvReleaseImage( &img2 );
- system("PAUSE"); return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment