Helpful Information
 
 
Category: Software Design
x , y coordinates

hi !
i need a little help

i have points all over the screen on x y coordinates
like this


. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

and so on all over the screen the x y coornites are known in advance
now wat i am trying to do is when ever a saqure is formed between four points user must get a message that a saqure is formed and when all the saqures are formed the loop must exit
i am unbale to do this
if some body can help i will be grate full
thanks
suhail

Where X1 and Y1 are starting points,
and X2 and Y2 are ending points...

If

(X1 - X2) = (Y1 - Y2)

Then

"You have a square"

Please do not post the same thing in multiple forums, this is considered double posting.

Originally posted by Waltjp
Where X1 and Y1 are starting points,
and X2 and Y2 are ending points...

If

(X1 - X2) = (Y1 - Y2)

Then

"You have a square"

To follow up on my previous post...

You should compare the absolute value of (X1 - X2) = absolute value of (Y1 - Y2)

If true, you have a square.

hi !

it did't help !

u say
X1-X2 =Y1-Y2
i have a saqure but no i only have twolines like this
look

i have a starting point
x1,y1 = 10,10
and ending point
x2,y2 = 15,15

now
x1-x2 =y1-y2
10-15 = 10-15
5=5

i have two lines not four i will get ths shape not a saqure ?
|
|
|______



ok thsi si wat my prog do

i have point all over the screen on x y coordinated the distace between one x,y point is 5 coloums x,y

. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .


now the user can move throgh these point with the help of cursor keys up ,down,right and left,

now if the user wants to selctt any point , user just press enter now that x,y point is selected lets say 10,10
now from ths point forward user can only move one point to right ,lleft ,up and down and select any of thses four points
now the user agin press the enter key and slects the up point at location x,y = 10,5

now a line is drawn from point 10,10 to 10,5 as soon as the line is drawn loops start again user can move throgh any point he wants and select the first point again at any x,y coornites and as soon as he selects that point he can move the courser to only one point ,up,down,right and left and as soon he select any point from these four points the line is dranm between thses points and so on it continues

now wat i wnat was how will i know if a saqure is formed anywhere between four points ??????

suhail

????????
no help ?
:(

You need to check your entire board for squares. I'm sure you have the board represented as a two-dimensional array. Here's some psudo-code for what I would do. Hope I am understanding you correctly.

1. You need to scan (using a loop) your board from upper left (point 1,1) to lower right (lets say 20,20). Locate a point on the board (POINT 1).

2. When you find POINT 1, check the entire row that POINT 1 is in for OTHER points. Also check the entire column that POINT 1 is in for OTHER points. I'd use an algorithm that loops through each point in the row, and whenever it finds a point in that same ROW, it checks to see if there is a point in the same COLUMN the same distance away (from POINT 1).

3. If you do the above and encounter a place where there is a POINT 1 with POINTS 2 and 3 that are the same distances away, (X2 - X1 = Y3 - Y1 & obviously Y1 = Y2 and X1 = X3), then check for a correct POINT 4 to make a square, at Y4 = Y3, X4 = X2.

Make sure you repeat step 2 for every matching row/column pair.
Make sure you repeat step 1 using every point as a starting point, or "POINT 1".

For reference I used the following:


A * * * * B
* * * * * *
* * * * * *
* * * * * *
* * * * * *
C * * * * D

with
A = X1, Y1
B = X2, Y2
C = X3, Y3
D = X4, Y4

Hope that helps get you started at least! If not, ask questions!

hi !
can u please explain point 2

2. When you find POINT 1, check the entire row that POINT 1 is in for OTHER points. Also check the entire column that POINT 1 is in for OTHER points. I'd use an algorithm that loops through each point in the row, and whenever it finds a point in that same ROW, it checks to see if there is a point in the same COLUMN the same distance away (from POINT 1).


wat do u mean by POINT 1 is in for OTHER points.????

suhailkaleem

What I meant in point 2 was...

Say you have a point at X,Y-coordinate 4,5. Think about the array as in rows and columns for a second.

Point 4,5 is in row 4 and column 5. Check Row 4 for other points.
Also check column 5 for other points.

Say you are using a 20 by 20 array.

For X goes from 1 to 20
{ Y = 5
look for point other than this point (being 4)
If found then
{
Measure distance from 4
Check for a third point, equidistant from 4,5 in column 4.
}
}

So for example, say you have a point 4,5 and a point at 10,5.

The above algorithm would go from 1 to 20.
At 10 it would find a point.
10 - 4 would equal 6.
It would look for a point at 4,11.
If found then you look for the 4th point of the square.

Hope that helps. Feel free to ask again if I didn't make sense.

hi !
the user can only select one point ahead from the current seletc point
lets say user selects point 20,20

now after selcting 20 ,20 user is restricted to select only these points

20,25 ( down cursor key )
20,15 ( up cursor key )
15,20 ( left cursor key )
25.20 ( rigt cursor key )

where 5 is the distance between every row and coloum

now lets say user selects a point 20,25 ( down cursor key )
now a line is drawn from 20,20 to 20,25 ,

now user can agin slect any point after selcting any point the user is agian restrced to select any four points (up ,down,right and left )
and again a ine is drwan between the first point selected and the second point slect and so on the user can draw lines all over the screen

now let say user select point 20,20
now there are four possiblities for making a saqure a saqure can be made to left ,right , up , and down right ?
so how will i know where did the user made the saqure and also if the saqure already made the saqure and user trys to make a another saqure over the current how will i stop the user ?

suhailkaleem

Originally posted by suhailkaleem
now let say user select point 20,20
now there are four possiblities for making a saqure a saqure can be made to left ,right , up , and down right ?
so how will i know where did the user made the saqure and also if the saqure already made the saqure and user trys to make a another saqure over the current how will i stop the user ?

suhailkaleem

After the user inserts any point, you run a function which checks the entire board for squares.

So even if you start at 20,20... and you don't know which way they are going to make the square, it won't matter. Just write a function which checks the entire board for squares.

As for what if the user tries to make another square over the currect one... well I believe the algorithm I provided will find all the squares possible, even ones that begin at the same point.

What code do you have thus far that works and I don't quite follow you when you say if you start at 20,20 the user can only put more points at 20,25 or 20,15 or 15,20... can't the user place points anywhere on the board?

mail sent to you with code
let me know if there is some problem

suhailkaleem

Why don't you just ask someone to write the code for you? Seems that's what you're looking for anyway.

For any 4 points selected

A = X1, Y1
B = X2, Y2
C = X3, Y3
D = X4, Y4

Compare the X value of point A to the other 3 points.

There should be only 1 other point with the same X value. If this is not true you do not have a square, stop here.

If it is true compare the Y value of point A to the other 3 points. Again, this should be true in only 1 case and should not be the point containing the same X value as found above.

This is what is known at this time.

A = X1, Y1
B = X1, ?
C = ?, Y1
D = ?, ?

Check the X value of C and D. If they are the same proceed.

Check the Y value of B and D. If they are the same proceed.

From what I've read I think that will suffice for your needs. If it's possible for the user to pick non-consecutive points you'll have to check the length of the sides and possibly the slope.

It would also be helpful if you shared your code here for everyone to take a look at.

Wow, I didn't even think of squares that were made on diagonals... that definitely complicates the problem. :eek:

I wish I could help further, but alas, I don't have a C compiler and I'm quite busy starting a new job...

If this is for a class (which I figure it is), it might be best to find a friend and work in groups. Thats what basically got me through my upper level comp sci classes. ;)

Good luck!

In the final months of my schooling I spent so much time at my friends house that his wife offered to make up a bed for me.

no ! it is not for a class i was getting bored so i decided to make some thing , so i tried out this but unluckly i got stuck so that is why i am asking you people to help me ,

and thanks Nichademus for giving it a try

any body else who would like to help me !

suhailkaleem

First off to make sure its a square you'll want to make sure that the two diagonals of the square have the same length. Easiest way to make sure its a square.

Think about pythagorus to do this. I could give you a formula, but from what I have read you wouldn't understand it. Have a think about pythagorus and how you can use it to find the length of the diagnols, then sub in the (x,y) values to your working...

If you want the square to be "flat", as in not on an angle, Y1 will equal Y2, and X1 will equal X3, with the coordinates being

(x1,y1) (x2,y2)
(x3,y3) (x4,y4)

Hope that helps.

i already know the lengs of the sides ,
ok here is the code see for yr self wat i am trying to tell you !


*************************************************

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)

{
clrscr();
int vary =9 ,varx =15 ,var_d =5;
int x,y,y1,x1,x2,y2,x3,y3,key,key2,key3,key_d=1,key_l=varx,key_u=vary,key_r=1,z ;


y =var_d ;
x=var_d ;
do
{
key3 =getch();


char t ='.',mask='ì',vars='l',sm ='*' ;
y1 =var_d ;
for (y3=0 ;y3<vary ; y3++ )
{
x1 =var_d ;
for(x3=0;x3<varx;x3++)
{
gotoxy(x1,y1);
textcolor(RED);
printf("%c",t);
x1=x1+var_d ;
}
y1=y1+var_d ;

}

do
{
//vars ='l' ;
// printf ("%c", vars );
key =getch ();
if (key =='x')
{
exit(1) ;
}
if (key==80)
if ( key_d < vary)
if (vars !='s')
{

{
{
key_d ++ ;
key_u--;

y =y+var_d;
gotoxy(x,y);
printf (" ") ;
gotoxy(x,y);
printf("%c",mask);
y=y-var_d ;
gotoxy(x,y) ;
printf("%c",t);
y=y+var_d;

gotoxy(10,40);
printf("x=%d ,y=%d",x,y);


}
}
}
if (key==72)
if( key_u < vary)
if ( vars !='s')
{
{
{
key_u++ ;
key_d--;

y =y-var_d;
gotoxy(x,y);
printf (" ") ;
gotoxy(x,y);
printf("%c",mask);
y=y+var_d ;
gotoxy(x,y);
printf("%c",t);
y=y-var_d;
gotoxy(10,40);
printf("x=%d ,y=%d",x,y);

}
}

}
if ( key==77 )

if ( key_r < varx )
if (vars !='s' )
{
{
{
key_r++;
key_l --;

x =x+var_d;
gotoxy(x,y);
printf (" ") ;
gotoxy(x,y);
printf("%c",mask);
x=x-var_d ;
gotoxy(x,y);
printf("%c",t);
x=x+var_d ;

gotoxy(10,40);
printf("x=%d ,y=%d",x,y);
}
}
}

if (key==75)
if ( key_l < varx)
if (vars != 's')
{
{
{
key_l++;
key_r--;

x =x-var_d;
gotoxy(x,y);
printf (" ") ;
gotoxy(x,y);
printf("%c",mask);
x=x+var_d ;
gotoxy(x,y) ;
printf("%c",t);
x=x-var_d;
gotoxy(10,40);
printf("x=%d ,y=%d",x,y);
}
} }


}



while (key!=13) ;





x2= x;
y2= y ;


do
{


if (key == 13)
{
vars ='s';
{

key2 =getch();
if (key2 =='x')
{
exit(1) ;
}
if ( key2==80)

if (vars =='s')
{
{

y=y-var_d ;
gotoxy(x,y) ;
printf("%c",sm);
x=x+var_d ;
y=y+var_d ;
gotoxy(x,y);
printf("%c",sm);
x=x-2*var_d ;
gotoxy(x,y);
printf("%c",sm);
x=x+var_d;
y=y+var_d ;
gotoxy(x,y);
printf("%c",mask);
y=y-var_d ;
key2 = getch();
if (key2 ==13)
{

for ( z =0 ; z <=var_d ; z++)
{
gotoxy(x2,y2);
printf ("|");
y2++ ;

}
// y2=y2-var_d ;
// int n=y2+var_d ;
// int p=0 ;
//int array[100] ;
// array[p] = n ;

// printf("%d",array[p]);
//p++ ;


vars='l' ;
}
}
}
if ( key2==72)
if (vars =='s')
{
{
y=y-var_d ;
gotoxy(x,y) ;
printf("%c",mask);
x=x+var_d ;
y=y+var_d ;
gotoxy(x,y);
printf("%c",sm);
x=x-2*var_d ;
gotoxy(x,y);
printf("%c",sm);
x=x+var_d;
y=y+var_d ;
gotoxy(x,y);
printf("%c",sm);
y=y-var_d ;

key2 = getch();
if (key2 ==13)
{

for ( z =0 ; z <=var_d ; z++)
{
gotoxy(x2,y2);
printf ("|");
y2-- ;
}
y2=y2+var_d ;
vars='l' ;
}

}
}

if ( key2==77)
if (vars =='s')
{
{
y=y-var_d ;
gotoxy(x,y) ;
printf("%c",sm);
x=x+var_d ;
y=y+var_d ;
gotoxy(x,y);
printf("%c",mask);
x=x-2*var_d ;
gotoxy(x,y);
printf("%c",sm);
x=x+var_d;
y=y+var_d ;
gotoxy(x,y);
printf("%c",sm);
y =y-var_d ;



key2 = getch();
if (key2 ==13)
{
for ( z =0 ; z <=var_d ; z++)
{
gotoxy(x2,y2);
printf ("-");
x2++ ;
}
x2 =x2-var_d ;
vars='l' ;
}
}
}


if ( key2==75)
if (vars =='s')
{
{
y=y-var_d ;
gotoxy(x,y) ;
printf("%c",sm);
x=x+var_d ;
y=y+var_d ;
gotoxy(x,y);
printf("%c",sm);
x=x-2*var_d ;
gotoxy(x,y);
printf("%c",mask);
x=x+var_d;
y=y+var_d ;
gotoxy(x,y);
printf("%c",sm);
y=y-var_d;



key2 = getch();
if (key2 ==13)
{
for ( z =0 ; z <=var_d; z++)
{
gotoxy(x2,y2);
printf ("-");
x2-- ;
}
x2 =x2+var_d ;
vars='l' ;
}

}
}

}
}


}

while (vars!='l');
}
while (key3!='x');

}



*************************************************


the code runs on tc ,
u will know wat the codes do if anything is not clear do ask me
suhailkaleem

no help ? :mad:










privacy (GDPR)