Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

c graphics programming


  • Please log in to reply
3 replies to this topic

#1 comet@earth

comet@earth

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:08:02 PM

Posted 12 February 2011 - 09:34 AM

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20

enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };

enum { FALSE, TRUE };
typedef unsigned int outcode;

outcode compute_outcode(int x, int y,
int xmin, int ymin, int xmax, int ymax)
{
outcode oc = 0;

if (y > ymax)
oc |= TOP;
else if (y < ymin)
oc |= BOTTOM;


if (x > xmax)
oc |= RIGHT;
else if (x < xmin)
oc |= LEFT;

return oc;
}

void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax)
{
int accept;
int done;
outcode outcode1, outcode2;

accept = FALSE;
done = FALSE;

outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
do
{
if (outcode1 == 0 && outcode2 == 0)
{
accept = TRUE;
done = TRUE;
}
else if (outcode1 & outcode2)
{
done = TRUE;
}
else
{
double x, y;
int outcode_ex = outcode1 ? outcode1 : outcode2;
if (outcode_ex & TOP)
{
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
}

else if (outcode_ex & BOTTOM)
{
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
}
else if (outcode_ex & RIGHT)
{
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
}
else
{
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (outcode_ex == outcode1)
{
x1 = x;
y1 = y;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
}
else
{
x2 = x;
y2 = y;
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
}
}
} while (done == FALSE);

if (accept == TRUE)
line (x1, y1, x2, y2);
}



void main()
{
int n;
int i, j;
int ln[MAX][4];
int clip[4];
int gd = DETECT, gm;

printf ("Enter the number of lines to be clipped");
scanf ("%d", &n);

printf ("Enter the x- and y-coordinates of the line-endpoints:\n");
for (i=0; i<n; i++)
for (j=0; j<4; j++)
scanf ("%d", &ln[i][j]);

printf ("Enter the x- and y-coordinates of the left-top and right-");
printf ("bottom corners\nof the clip window:\n");
for (i=0; i<4; i++)
scanf ("%d", &clip[i]);

initgraph (&gd, &gm, "..//bgi");

rectangle (clip[0], clip[1], clip[2], clip[3]);
for (i=0; i<n; i++)
line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);
getch();
cleardevice();
rectangle (clip[0], clip[1], clip[2], clip[3]);
for (i=0; i<n; i++)
{
cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],
clip[0], clip[1], clip[2], clip[3]);
getch();
}
closegraph();
}
This is the cohen sutherland alg.Please help me with the first part where the enum is declared and the OR operation is done.What the hell is happening here.
Please Please help me I have my test on monday

BC AdBot (Login to Remove)

 


#2 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:09:32 AM

Posted 12 February 2011 - 11:34 AM

If you are talking about compute_outcode() function, then it is just returning an unsigned int value.

First note that these values in binary form.
TOP = 0x1 = 0001
BOTTOM = 0x2 = 0010
LEFT = 0x4 = 0100
RIGHT = 0x8 = 1000

it initializes oc to 0.

Then checks if y is within ymax and ymin, if not then it uses bitwise OR to set oc to TOP or BOTTOM.
[Because oc = 0, oc |= TOP is similar to saying oc = TOP.]

Next it checks of x is within xmax and xmin and accordingly performs bitwise OR.
Because TOP, BOTTOM, LEFT and RIGHT all have different bits, performing a bitwise OR on any of them is like adding them.

You can perform the same thing using this function :

outcode compute_outcode(int x, int y,int xmin, int ymin, int xmax, int ymax){
     outcode oc = 0;
     if (y > ymax) oc = TOP;
    else if (y < ymin) oc = BOTTOM;
 
     if (x > xmax) oc += RIGHT;
    else if (x < xmin) oc += LEFT;
 
    return oc;
}

Did I confuse you even more? If yes, then read more about bits and bitwise OR http://en.wikipedia.org/wiki/Bitwise_operation

Edited by Romeo29, 12 February 2011 - 11:38 AM.


#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:08:32 AM

Posted 12 February 2011 - 12:06 PM

Was this just taught to you on Friday? :blink:

Edited by groovicus, 12 February 2011 - 12:59 PM.


#4 comet@earth

comet@earth
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:08:02 PM

Posted 12 February 2011 - 01:39 PM

The C compiler takes input(maybe hex or dec) and converts it to binary to perform operations,am I right?
@Groovicus:No sir on monday.I tried on my own but couldn't get the whole logic.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users