Graphical intersection of lines in C - c

I have a homework.
The homework is: There are 3 lines, theirs end with squares. Firstly The program has to view a circle is the lines are cut each other. (In other way: Take the intersection of the 3 lines). Secondly, the program has to change the background, along the lines. Each line's both side define the background with a colour. And how rotating the lines, together with them, the background colour changing.
There are 3 lines, and 6 background colour. The border of background color is along the lines.
The programming enviroment is the DevC++ (we must use the c++ console applicaton, but in the lesson we not coding in c++, just c...)
Youtube video about the exercise/homework
I've tried implement of the lines' intersection, but It doesn't work very well.
And I don't have any idea, how can I implement of the colourful background change.
What kind of knowledge is needed for it?
I would like to if somebody can suggest to me some: algorithm, webpage, tutorial, sourecode, anything what can help me. Or what is the name of my homework in english ( to google search)
Cause I don't think, my solution is the best way to prepare my homework (maybe it won't succes)
Here the code, that I have did up to now (but it not perfect. The intersection of lines is not perfectly. It's not a beautiful solution, sorry I am not an expert C programmer):
sourcode in english
PONT = point, dot
PONTH = aggregation of points
atir = rewrite
metszilleszt = fitting of intersection
szakasz = section, phase... (there is too many in english-hungarian) or platoon :-D
eger = mouse
egérkezelés = mouse control
balgomb = left button of mouse
# include "graphics.h"
# include <conio.h>
#include <stdio.h>
typedef struct {
float x1,x2,x3;
} PONTH;
typedef struct {
double x,y;
}PONT;
PONTH atir(PONT A){
PONTH C;
C.x1=A.x;
C.x2=A.y;
C.x3=1;
return C;
}
PONTH metszilleszt(PONTH A,PONTH B){
PONTH C;
C.x1=(A.x2*B.x3)-(A.x3*B.x2);
C.x2=-(A.x1*B.x3)+(A.x3*B.x1);
C.x3=(A.x1*B.x2)-(A.x2*B.x1);
return C;
}
int main()
{
//PONT szakasz[4]={100,50,300,200,30,130,140,170};
PONT szakasz[6]={100,50,300,200,30,130,140,170,30,70,210,40};
int ap;
int gd,gm;
int page =0;
gd=VGA;gm=VGAMED;
initgraph(&gd,&gm,"");
PONTH A,B,C,D,E,F;
PONTH tmp1,tmp2,tmp3,tmp4,tmp5,tmp6;
PONT pont;
for(;;){
setactivepage(page);
cleardevice();
A=atir(szakasz[0]);
B=atir(szakasz[1]);
C=atir(szakasz[2]);
D=atir(szakasz[3]);
E=atir(szakasz[4]);
F=atir(szakasz[5]);
tmp1=metszilleszt(A,B);
tmp2=metszilleszt(C,D);
tmp3=metszilleszt(E,F);
tmp4=metszilleszt(tmp2,tmp1);
tmp5=metszilleszt(tmp3,tmp1);
tmp6=metszilleszt(tmp3,tmp2);
pont.x=int (tmp3.x1/tmp3.x3);
pont.y=int (tmp3.x2/tmp3.x3);
//printf("%f %f\n",pont.x,pont.y);
// good
if((((tmp4.x2/tmp4.x3)>=szakasz[0].y) && ((tmp4.x2/tmp4.x3)<=szakasz[1].y)) &&
(((tmp4.x1/tmp4.x3)>=szakasz[0].x) && ((tmp4.x1/tmp4.x3)<=szakasz[1].x)) ||
(((tmp4.x2/tmp4.x3)>=szakasz[0].y) && ((tmp4.x2/tmp4.x3)<=szakasz[1].y)) &&
(((tmp4.x1/tmp4.x3)<=szakasz[0].x) && ((tmp4.x1/tmp4.x3)>=szakasz[1].x)))
{
setcolor(RED);
fillellipse(int (tmp4.x1/tmp4.x3),int (tmp4.x2/tmp4.x3),5,5);
}
if((((tmp5.x2/tmp5.x3)>=szakasz[0].y) && ((tmp5.x2/tmp5.x3)<=szakasz[1].y)) &&
(((tmp5.x1/tmp5.x3)>=szakasz[0].x) && ((tmp5.x1/tmp5.x3)<=szakasz[1].x)) ||
(((tmp5.x2/tmp5.x3)>=szakasz[0].y) && ((tmp5.x2/tmp5.x3)<=szakasz[1].y)) &&
(((tmp5.x1/tmp5.x3)<=szakasz[0].x) && ((tmp5.x1/tmp5.x3)>=szakasz[1].x)))
{
setcolor(RED);
//fillellipse(int (tmp5.x1/tmp5.x3),int (tmp5.x2/tmp5.x3),5,5);
fillellipse(int (tmp5.x1/tmp5.x3),int (tmp5.x2/tmp5.x3),5,5);
}
if((((tmp6.x2/tmp6.x3)>=szakasz[0].y) && ((tmp6.x2/tmp6.x3)<=szakasz[1].y)) &&
(((tmp6.x1/tmp6.x3)>=szakasz[0].x) && ((tmp6.x1/tmp6.x3)<=szakasz[1].x)) ||
(((tmp6.x2/tmp6.x3)>=szakasz[0].y) && ((tmp6.x2/tmp6.x3)<=szakasz[1].y)) &&
(((tmp6.x1/tmp6.x3)<=szakasz[0].x) && ((tmp6.x1/tmp6.x3)>=szakasz[1].x)))
{
setcolor(RED);
fillellipse(int (tmp6.x1/tmp6.x3),int (tmp6.x2/tmp6.x3),5,5);
}
//else{ setcolor(RED);
// fillellipse(int (tmp3.x1/tmp3.x3),int (tmp3.x2/tmp3.x3),5,5); }
/* Egerkezeles */
if (!balgomb) ap = getactivepoint((pont2d*)szakasz,6,6);
if (ap >= 0 && balgomb)
{
szakasz[ap].x = egerx;
szakasz[ap].y = egery;
}
/* Egerkezeles vege */
setcolor(WHITE);
line((int)szakasz[0].x,(int)szakasz[0].y,(int)szakasz[1].x,(int)szakasz[1].y);
rectangle((int)szakasz[0].x,(int)szakasz[0].y, (int)szakasz[0].x+4, (int)szakasz[0].y+4);
rectangle((int)szakasz[1].x,(int)szakasz[1].y, (int)szakasz[1].x+4, (int)szakasz[1].y+4);
line((int)szakasz[2].x,(int)szakasz[2].y,(int)szakasz[3].x,(int)szakasz[3].y);
rectangle((int)szakasz[2].x,(int)szakasz[2].y, (int)szakasz[2].x+4, (int)szakasz[2].y+4);
rectangle((int)szakasz[3].x,(int)szakasz[3].y, (int)szakasz[3].x+4, (int)szakasz[3].y+4);
line((int)szakasz[4].x,(int)szakasz[4].y,(int)szakasz[5].x,(int)szakasz[5].y);
rectangle((int)szakasz[4].x,(int)szakasz[4].y, (int)szakasz[4].x+4, (int)szakasz[4].y+4);
rectangle((int)szakasz[5].x,(int)szakasz[5].y, (int)szakasz[5].x+4, (int)szakasz[5].y+4);
setvisualpage(page);
page = 1-page;
if (kbhit()) break;
}
getch();
closegraph();
return(0);
}

Regarding the intersections of lines, you should read the article of Loren Shure and Lucio Cetto from Matworks on Loren's blog:
Part 1
Part 2
It's in Matlab but the principles are the same.

Related

C/Ncurses how to wrap text in a textfield

I would like to wrap a short text in a Text Box in Ncurses, but somehow my text keeps going off screen. How can I wrap the text so it (automatically) goes to a new line when reaching the end of the screen on the right?
I tried playing with '\n' and setting limits but witout results. Any tips what I am doing wrong? See below code for what is going on.
Thanks from a beginner programmer.
#include <ncurses.h>
#include <string.h>
void text(WINDOW* textborder, int wymax, int wxmax, char text5[], int size)
{
for (int i=0;i<size;i++)
{
mvwaddch(textborder,2,i+i, text5[i]);
if (i==wxmax)
{
mvwaddch(textborder,2,i+i, '\n');
}
}
}
int main()
{
char text5[]={"Somebody is watching over us... controlling us. It's true, I tell you. It's true! We are merely sprites that dance at the beck and call of our button-pressing overlord. This is a video game. Don't you see? We are characters in a video game."};
int size;
size=strlen(text5);
int wymax; int wxmax;
initscr();
WINDOW* textborder=newwin(LINES/4, COLS, LINES-LINES/4, 0);
box(textborder,-1,-1);
getmaxyx(textborder, wymax,wxmax);
wxmax=wxmax-4;
text(textborder, wymax, wxmax, text5, size);
wgetch(textborder);
endwin();
return 0;
}
In theory the text should wrap itself. I think your issue may be coming from using mvwaddch, ch generally causes the text not to wrap. This may help Ncurses no-wrap mode when adding strings to window. Sorry I can't be more helpful :)
Applications that draw a box with a border in curses do this using two windows, one within the other. The outer box gets the border; the inner box gets the text. Text printed within the inner box does not affect the outer box.
For example, some of the demo/example programs in ncurses do this, e.g., test_addstr creates look (for the box) and show (for the text):
limit = LINES - 5;
if (level > 0) {
look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
work = newwin(limit - 2, COLS - (2 * level), 1, level);
show = newwin(4, COLS, limit + 1, 0);
box(look, 0, 0);
wnoutrefresh(look);
limit -= 2;
} else {
work = stdscr;
show = derwin(stdscr, 4, COLS, limit + 1, 0);
}
keypad(work, TRUE);

GLut in C - Collision isn't working all the time

I'm currently working on a side scroller, my player can shoot, as do the enemies. If I hit an enemy,it should disappear. Unfortunately, it doesn't work all the time. Sometime it does, sometime not. Sometimes, it work perfectly like here :
Ennemi 30 =eX:37 - eY:51
Shot 12=sX:37 - sY:52
====
BOUM
and for some reasons, it happens that my shot go trough the enemy like it doesn't exist.
Here's how I detect collision :
void checkCollisionShot(ListShot* list,ListEnnemy* listE){
if (list == NULL || listE == NULL){
exit(EXIT_FAILURE);
}
Shot *getSh=list->first;
Ennemy *getEn=listE->first;
if (list->first!= NULL && listE->first!=NULL)
{
int i,j;
for(i=0;i<list->nbr;i++){
int sX=getSh->obj.posi.x;
int sY=getSh->obj.posi.y;
for(j=0;i<listE->nbr;i++){
int eX=getEn->obj.posi.x;
int eY=getEn->obj.posi.y;
if(getEn->obj.friend!=getSh->obj.friend){
if(sX<eX+3 && sX>=eX){
if(sY<eY+3 && sY>=eY){
printf("Boum\n");
deleteEnnemy(listE,eX,eY);
deleteShot(list,sX,sY);
}
}
}
getEn=getEn->next;
}
getSh=getSh->next;
}
}
}
I'm working with simple linked list : One for the shots, another one for the enemies. I'm browsing each element, one by one, and compare each element from a list with the position of the other list.
Is the mistake coming from this function, or does it come from one of the GLut function (-like- the refresh function) ?
Okay, I found it ! The declaration of getEn should be reset with every loop. The code look like this now :
void checkCollisionShot(ListShot* list,ListEnnemy* listE){
if (list == NULL || listE == NULL)
{
exit(EXIT_FAILURE);
}
Shot *getSh=list->first;
//GETEN IS NOT HERE ANYMORE<-----------------------------
if (list->first!= NULL && listE->first!=NULL)
{
while(getSh!=NULL){
int sX=getSh->obj.posi.x;
int sY=getSh->obj.posi.y;
Ennemy *getEn=listE->first;//<------------------THE DIFFERENCE IS HERE
while(getEn!=NULL){
int eX=getEn->obj.posi.x;
int eY=getEn->obj.posi.y;
if(getEn->obj.friend!=getSh->obj.friend){
if(sX<eX+3 && sX>=eX){
if(sY<eY+3 && sY>=eY){
//exit(0);
printf("\n\nBOUM\n");
deleteEnnemy(listE,eX,eY);
deleteShot(list,sX,sY);
break;
}
}
}
getEn=getEn->next;
}
getSh=getSh->next;
}
}
}
For the record, only the first element of the ListShot was compared with every element of the ListEnnemy. Now, every object is compared with each other.

Trouble registering screen boundaries for certain inputs (down and right)

I am creating a scrolling shooter for DMG using gbdk, it is based off some youtube tutorials and this example. In fact the link is the skeleton of my program. My issue is that the screen boundary conditions aren't working properly for down and right inputs. For up and left, they work correctly however, and the code for those is basically the exact same. I have also compiled the code from the link above, and it works correctly there. Apologies in advance, I have a childish sense of humor, so the game is penis-based.
The main differences between the skeleton code and mine is that I use a meta-sprite for the player, and an array for the x and y coordinates of the player. I have tried using individual integers for the locations and changing the bounds of the screen, but nothing seems to work.
#include <gb/gb.h>
#include <stdio.h>
#include "gameDicks.c"
#include "DickSprites.c"
#define SCREEN_WIDTH 160
BOOLEAN ishard = TRUE, playing = TRUE;
struct gameDicks flacid;
struct gameDicks hard;
INT8 spritesize = 8, dicklocation[2] = {20, 80};
int i;
void moveGameDicks(struct gameDicks* Dick, UINT8 x, UINT8 y){
move_sprite(Dick->spriteids[0], x, y);
move_sprite(Dick->spriteids[1], x + spritesize, y);
move_sprite(Dick->spriteids[2], x, y + spritesize);
move_sprite(Dick->spriteids[3], x + spritesize, y + spritesize);
}
void setuphard(INT8 dicklocation[2]){
hard.x = dicklocation[0];
hard.y = dicklocation[1];
hard.width = 16;
hard.height = 16;
//load sprites
set_sprite_tile(0,0);
hard.spriteids[0] = 0;
set_sprite_tile(1,1);
hard.spriteids[1] = 1;
set_sprite_tile(2,2);
hard.spriteids[2] = 2;
set_sprite_tile(3,3);
hard.spriteids[3] = 3;
}
void init_screen()
{
SHOW_BKG;
SHOW_SPRITES;
DISPLAY_ON;
}
void init_player()
{
SHOW_SPRITES;
set_sprite_data(0, 8, DickSprites);
setuphard(dicklocation);
}
void input()
{
if (joypad() & J_UP && dicklocation[1])
{
if (dicklocation[1] <= 16){
dicklocation[1] = 16;
}
else{
dicklocation[1]--;
}
}
if (joypad() & J_DOWN && dicklocation[1])
{
if (dicklocation[1] >= 150){
dicklocation[1] = 150;
}
else{
dicklocation[1]++;
}
}
}
void update_sprites()
{
moveGameDicks(&hard, dicklocation[0], dicklocation[1]);
}
int main()
{
init_screen();
init_player();
init_screen();
while(playing)
{
wait_vbl_done(2);
input();
update_sprites();
}
return 0;
}
What I expect is to be able to move the player up to y = 16, and down to y = 150. When it hits these values, it stops moving until you go the other direction. Instead, what I see happen is that the up direction works as expected, but as soon as the down key is pressed - no matter the y-location - the player is immediately sent to the bottom of the screen. From there, pressing up sends it to the very top. Further, the player can only move from the top position to the bottom, and not scroll in between. I'm baffled by this because the conditions are the exact same (except for the y-values), so I don't understand why they behave so differently.
Using an unsigned int may help here as an 8-bit integer will only hold values from -128 to 127, which might cause undefined behaviour when you compare it with over 150, pushing it to a negative value?
You have defined dicklocation as an INT8, when it would be better as a UINT8 or even longer if you plan on ever having a screen size larger than 255 bytes.

C program shows no error but doesn't show output

Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload.
Minimum dimension of the picture can be L x L, where L is the length of the side of square.
Now Roy has N photos of various dimensions.
Dimension of a photo is denoted as W x H
where W - width of the photo and H - Height of the photo
When any photo is uploaded following events may occur:
If any of the width or height is less than L, user is prompted to upload another one. Print "UPLOAD ANOTHER" in this case.
If width and height are both large enough and
(a) if the photo is already square then it is accepted. Print "ACCEPTED" in this case.
(b) else user is prompted to crop it. Print "CROP IT" in this case.
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int len; /* len is the length of the side of square */
scanf("%d",&len);
int test;
scanf("%d",&test);
while(test--)
{
int w,h;
/* w - width of the photo and h- Height of the photo */
scanf("%d %d",&w,&h);
if(w==len && h==len)
{
printf("ACCEPTED\n");
}
else if(w>len || w==len && h>len || h==len)
{
printf("CROP IT\n");
}
else
{
printf("UPLOAD ANOTHER\n");/* print */
}
}
return 0;/* success */
}
If your problem is that you're seeing no output at all, it's likely because it's running in an IDE and the output window is closing before you can see it. Judicious use of a getchar() at the end of main() may be all you need to fix this, though I'd probably prefer to just run it from the command line.
In any case, since && has a higher precedence than ||, your second condition is effectively:
else if ((w > len) || (w == len && h > len) || (h == len))
which is clearly wrong since having a photo with width more than what was necessary would result in a request for cropping regardless of the other sub-conditions.
You would be better off following the textual specs more closely so that you can actually check it more easily. That would entail something like (with slightly simplified specs still meeting the original intent):
If any of the width or height is less than L, user is prompted to upload another one.
Otherwise both are large enough. If the photo is already square then it is accepted.
Otherwise user is prompted to crop it.
The code for that is a much simpler:
if ((w < len) || (h < len)) {
puts ("UPLOAD ANOTHER");
} else if (h == w) { // Both large enough otherwise previous
puts ("ACCEPTED"); // condition would have been true.
} else {
puts ("CROP IT");
}
Due to operator precedence,
else if(w>len || w==len && h>len || h==len)
is equivalent to:
else if(w>len || (w==len && h>len) || h==len)
What you need to use is:
else if( (w>len || w==len) && (h>len || h==len) && (w == h) )
You can simplify that to:
else if( w >= len && h >= len && w == h )
which can be further simplified to:
else if( w >= len && w == h )
It will show output but the program runs faster than you expect. It will close as soon as the output is displayed . It will not wait till you read it.
So use getch(); declared in conio.h to get a single key press as input after you have printed all the output (right before the return statement. It will make the program wait for the user to press a key before exiting.
here is the screenshot. it is running without error on me.
NOTE: This is not an answer. But this is just the only way to give picture example :) no need to up
Maybe you only need to put getchar(); at the end of the code before return to let the screen to pause for a while. it is just exiting very fast

Equation returning 1.#QO

I've tried searching for anything similar about my issue on several websites, including this one, but none I've found so far are similar. After searching about the term 1.#QO, I found something about quiet NaN, but I'm new to C in general, so I don't really understand the issue .
I'm trying to take the x and y values of a joystick, and when then use a formula for distance to find the distance between the joystick's position, and the joystick's natural resting position (0,0).
If it matters, I'm trying to do this in RobotC.
#pragma config(Hubs, S1, HTMotor, none, none, none)
#pragma config(Sensor, S1, , sensorI2CMuxController)
#pragma config(Motor, mtr_S1_C1_1, DriveMotor, tmotorTetrix, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#include "JoystickDriver.c"
int calculateDrivePower(int joyStickX, int joyStickY)
{
if (joyStickX != 0 & joyStickY != 0)
{
int joyDistance = (sqrt(pow((joyStickX - 0),2)+ pow((-joyStickY - 0),2)));
joyDistance = ((joyDistance/127)*100);
return (joyDistance);
}
else
{
return 0;
}
}
task main()
{
int previousJoySlope = 0;
int TurnSpeed = 70;
while (true)
{
getJoystickSettings(joystick);
if (abs(joystick.joy1_x1) > 10 || abs(joystick.joy1_y1) > 10)
{
writeDebugStreamLine("Test successful.");
motor[DriveMotor] = calculateDrivePower(joystick.joy1_x1,joystick.joy1_y1);
}
}
}
If anyone could provide any insight, that'd be fantastic, thanks.
if (joyStickX != 0 & joyStickY != 0)
{
int joyDistance = (sqrt(pow((joyStickX - 0),2)+ pow((-joyStickY - 0),2)));
joyDistance = ((joyDistance/127)*100);
return (joyDistance);
}
The first issues that appears is the conditional within the if statement. The you have a single & that most likely should be &&:
if (joyStickX != 0 && joyStickY != 0)
(note: likely should be is used above because you can provide a conditional using a logical & of the tests joystickx != 0, but in this case it provides the same result. In that case, I would suggest the more readable && be used)
The next part of the code is simply the vector distance between 0,0 and the present position of the joystick. Of the general form dist^2 = (x2-x1)^2 + (y2-y1)^2 in your case x1,y1 = 0,0. Taking the square root of both sides provides dist = sqrt((x2-x1)^2 + (y2-y1)^2), or in C notation:
dist = (sqrt(pow((joyStickX - 0),2)+ pow((-joyStickY - 0),2)));
Next you have a scaling applied of dist = dist * 100/127 which provides the final distance returned. Hopefully this will help you understand what the code is doing.

Resources