Every time I want to fil each circle with differnt pattern, and tried writing code next to differn radius, couldnt do, need help
Output
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
initgraph(&gd, &gm,"C:\\TURBOC3\\BGI");
circle(180,170,20);
circle(180,170,40);
setfillstyle(HATCH_FILL,RED);
floodfill(180, 170, WHITE);
circle(180,170,60);
circle(180,170,80);
circle(180,170,100);
getch();
closegraph();
}
Related
I have a couple of ncurses windows and am trying to move the cursor, to the end of the current line of text.
In other words, I want to move to the first non-blank character from the end of the window.
Example:
If I have a line of text in a ncurses window
I need help! Please! !
^
|
cursor
I want to move the cursor last character
I need help! Please! !
^
|
cursor
My best attempt is this:
#include <ncurses.h>
int main()
{
initscr();
refresh();
WINDOW* w = newwin(100, 100, 0, 0);
wprintw(w, "I need help! Please! !");
wmove(w, 0, 3);
wrefresh(w);
// MY ATTEMPT
int maxX, maxY, x, y;
getmaxyx(w, maxY, maxX);
getyx(w, y, x);
wmove(w, y, maxX);
while (winch(w) == ' ') {
wmove(w, y, maxX-1);
}
wrefresh(w);
// END OF MY ATTEMPT
getch();
delwin(w);
endwin();
return 0;
}
Which I think is logically sound, but is not working, and I am not sure why (the position of cursor isn't changing at all)
How do I do this? Is there an easy way I am missing? Why is my solution not working?
You never update your x position inside the loop, so you repeatedly move to one before the right edge of your window.
Assuming you do not use maxX elsewhere, simply pre-decrement it within the loop.
while((winch(w) & A_CHARTEXT) == ' ') {
wmove(w, y, --maxX);
}
Note that you should also use the A_CHARTEXT bit mask to extract the char from a chtype.
A very rough example of your method working, using stdscr:
#include <ncurses.h>
int main(void) {
initscr();
noecho();
cbreak();
while (1) {
clear();
mvprintw(10, 5, "Hello world");
move(0, 0);
int my,mx;
getmaxyx(stdscr, my, mx);
move(10, mx);
while ((inch() & A_CHARTEXT) == ' ')
move(10, --mx);
refresh();
napms(100);
}
endwin();
}
#include <stdio.h>
#include <math.h>
#include<time.h>
#include<stdlib.h>
#include <GL/glut.h>
clock_t t;
double X1, Y1, X2, Y2;
void delay(int number_of_seconds)
{
// Converting time into milli_seconds
int milli_seconds = 1000 * number_of_seconds;
// Stroing start time
clock_t start_time = clock();
// looping till required time is not acheived
while (clock() < start_time + milli_seconds)
;
}
float round_value(float v)
{
return floor(v + 0.5);
}
void MP(void)
{
double x,y,p;
x=X1;
y=Y1;
double dx=(X2-X1);
double dy=(Y2-Y1);
p=2*dy-dx;
double steps;
/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);
/* Plot the points */
glBegin(GL_POINTS);
/* Plot the first point */
glVertex2d(x,y);
int k;
/* For every step, find an intermediate vertex */
/* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
while(x<X2)
{
if(p>=0)
{
glVertex2d(round_value(x), round_value(y));
y=y+1;
p=p+2*dy-2*dx;
}
else
{
glVertex2d(round_value(x), round_value(y));
p=p+2*dy;
}
x=x+1;
}
glEnd();
glFlush();
}
void LineDDA(void)
{
glClear(GL_COLOR_BUFFER_BIT);
double dx=(X2-X1);
double dy=(Y2-Y1);
double steps;
float xInc,yInc,x=X1,y=Y1;
/* Find out whether to increment x or y */
steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);
/* Plot the points */
glBegin(GL_POINTS);
/* Plot the first point */
glVertex2d(x,y);
int k;
/* For every step, find an intermediate vertex */
for(k=0;k<steps;k++)
{
x+=xInc;
y+=yInc;
/* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
glVertex2d(round_value(x), round_value(y));
}
glEnd();
glFlush();
}
void Init()
{
/* Set clear color to white */
glClearColor(1.0,1.0,1.0,0);
/* Set fill color to black */
glColor3f(0.0,0.0,0.0);
/* glViewport(0 , 0 , 640 , 480); */
/* glMatrixMode(GL_PROJECTION); */
/* glLoadIdentity(); */
gluOrtho2D(0 , 640 , 0 , 480);
}
void main(int argc, char **argv)
{
char ch='n';
int choice;
/* Initialise GLUT library */
glutInit(&argc,argv);
/* Set the initial display mode */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
/* Set the initial window position and size */
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
do
{
printf("Enter two end points of the line to be drawn:\n");
printf("\n************************************");printf("\nEnter Point1( X1 , Y1):\n");
scanf("%lf%lf",&X1,&Y1);
printf("\n************************************");
printf("\nEnter Point1( X2 , Y2):\n");
scanf("%lf%lf",&X2,&Y2);
glFlush();
/* Create the window with title "DDA_Line" */
printf("!----------------Menu---------------!\n");
printf("!---------------1.DDA---------------!\n");
printf("!------------2.Brehanham------------!\n");
scanf("%d",&choice);
glutCreateWindow("Compare b/w DDA and Bresenham");
/* Initialize drawing colors */
Init();
/* Call the displaying function */
t=clock();
if(choice==1)
glutDisplayFunc(LineDDA);
else if(choice==2)
glutDisplayFunc(MP);
else
printf("\nWrong choice");
t=clock()-t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("Algorithm took %f seconds to execute \n", time_taken);
printf("Question?\n");
scanf(" %c",&ch);
delay(3000);
glutDestroyWindow(1);
}while(ch=='Y');
/* Keep displaying until the program is closed */
glutMainLoop();
}
I am trying to implement comparison between to algorithms. (Midpoint and DDA). But when I try to open the window it takes screenshot of background instead of the actual algorithm. How do I flush the frame buffer to avoid this? I put glFlush in multiple places but that does not seem to do the trick.
Any help would be deeply appreciated.
I am afraid you misunderstand how to use GLUT.
What glutMainLoop does is invoke your set glutDisplayFunc every time the window is asked to repaint.
What you describe as "taking a screenshot of the background" is actually "my window is not being repainted".
One way forward is as follows:
First collect all user input (coordinates and drawing mechanism) and store them in memory (global variables will do, initially).
Next, set your glutDisplayFunc to a function of your own that will render the stored inputs to screen, keeping in mind the drawing algorithm selected by the user.
Finally, enter the glutMainLoop. This will run until the user exits the program, at which point the loop will exit and you can destroy the window.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
double gen(double dS) //Function for a random double variable
{
double dx=0;
dx=rand()%200+1;
dS=dx/100;
return(dS);
}
int main()
{
double dZahl=0;
double dTokens=1;
srand((unsigned)time(NULL));
double dSummand=0;
int iGame=0;
for(;;)
{
printf("Deine Tokens:%.2lf\n", dTokens); //This doesn't really matter
printf("Was m%cchtest du tun?\n[1]:Generiere Tokens\n[2]:Spiele ein Minigame\n", 148);
fflush(stdout);
scanf("%d", &iGame);
fflush(stdin);
switch (iGame)
{
case 1:
{
gen(dSummand); //function in use
dTokens=dTokens+dSummand;
printf("\nDu hast %.2lf Tokens generiert!\n", dSummand); //the output of the value
fflush(stdout);
}
break;
}
}
getch();
}
The problem is that dSummand doesn't get the value from dS. Does anyone know what the problem is because I was trying to fix it but I couldn't figure it out.
I am trying to get a random double value with a function for my program. I thought it would work like this but unfortunately it didn't.
If you want the value for dS to come out of the function modified you have to pass a pointer.
double gen(double *dS) {
*dS=dx/100;
and then
gen(&dSummand);
//Program to implement Basic Incremental Algorithm
//Working on ubuntu
#include <GL/glut.h>
#include<stdlib.h>
#include <stdio.h>
GLfloat x0,x1,y0,y1; //Input variables taken as global
int flag=1; //variable for display1()
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0,0.0,500.0);
}
void PutPixel(GLfloat x,GLfloat y)
{
glBegin(GL_POINTS);
glVertex2f(x,y); //To display pixels on-screen
glEnd();
glFlush();
}
void display1(void)
{
if (flag==1)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.7,1.0,1.0);
GLfloat m,c,y;
GLfloat i; //BIA algorithm
m=(y1-y0)/((float)(x1-x0));
c=y1-m*x1;
for (i=x0; i<=x1; i+=0.01)
{
y=c+m*i;
PutPixel(i,y);
}
flag++;
}
}
void Input()
{
printf("Enter the co-ods\n");
scanf("%f %f",&x0,&y0);
scanf("%f %f",&x1,&y1);
}
int main(int argc, char **argv)
{
Input();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("BIA");
init();
glutDisplayFunc(display1);
glutMainLoop();
return 0;
}
I have initailaized flag as a global variable in the beginning and set it to 1. flag is used in the display1() to ensure that it executes only once. This is just one way I was trying to ensure that the output gets displayed.
Can anyone, please HELP!
Why doesn't the program stop taking input ?
It's working. I'm still unsure about which edit or change bought about it. But it's working!! it's displaying some output
I have a the following piece of code using ncurses. I would like to know whether I can use a single move function to print a few lines.
For Example:
move(25,25);
printw("Line 1\n");
printw("Line 2\n");
Line 1 prints at (25,25) location but Line 2 prints at (26,0) if I don't use move(26,25). Can I avoid the second move and still print Line 2 at (26,25)????
You can define a new window if what you want to print has to be aligned. Shortly :
#include <ncurses.h>
int main()
{
WINDOW* mywin;
initscr();
cbreak();
keypad(stdscr, TRUE);
int height=15;
int width=30;
int starty=25;
int startx=25;
printw("F9 to exit");
refresh();
mywin = newwin(height, width, starty, startx);
mvwprintw(mywin,0,0,"First line\n");
wprintw(mywin,"Second line");
wrefresh(mywin);
while(getch() != KEY_F(9)) {}
endwin();
return 0;
}
If this approach does not fit, then you'll have to move manually to next position you want to print.