# NTNUJAVA: Virtual Physics Laboratory: Enjoy the fun of Physics

• Line Patterns by:Paraliasat 2007/12/11 16:36Hallo everybody! ;D

Newbie here ;D

I have an exercise from my University and i don't know how to do it.

Here it is...

I want my applet to read from the file coord_data_file.txt an Integer for example N=5 (the number of points) and the five points,for example
16384 19200
16384 18666
16384 32000
16384 21761
10000 10000

The points must be between 0 and 32768
To read the points we must use the nextInt() method of java.util.Scanner

And what the program should do is the following...

We must draw the points calling the PointPlotter.java(i will give you a .zip with all the files included)
After that we choose a point (we name it p).We compare the angle that it makes with each one of the other points (we name it q) with the compareTo method.
Then,we sort the points according to the angle that they make with the p.
We check if 3 or more of the sorted points have the same angle to p.If we find them then all these points belog to the same straight line.

So,please,can anyone help me ??? ???

And some images here:[URL=http://img227.imageshack.us/my.php?image=assignment2ik6.jpg][IMG]http://img227.imageshack.us/img227/1653/assignment2ik6.th.jpg[/img][/URL][URL=http://img507.imageshack.us/my.php?image=assignment2ga3.jpg][IMG]http://img507.imageshack.us/img507/1404/assignment2ga3.th.jpg[/img][/URL]

See 10 replies click

1. Re: Line Patterns by:hwangat 2007/12/11 22:43I believe it is your homework. You can not just say you do not know how to do it.

You need to indicate where you need help. Can you read data from the file?
Do you know how to use nextInt() method. If you do not know, try to find information.
Do you know how to draw the points by calling the PointPlotter.java provided by your teacher. ...

I will try to help. But I can not do it for you. If I do it for it. Then it is not really good for you, and it is not fair,either.
You need to learn the skill to be able to find a job later on in real world.  ;)

2. Re: Line Patterns by:Paraliasat 2007/12/12 19:36Thanks for answering.

The point with my homework is that the teacher has never told us anything about these methods, or gave us an example, so i don't know how to do it.

Anyway,i'm not asking you to do it for but help me with it.

I'll try to open the txt file and read from it,but i don't know how to use the nextInt() method, or draw the points.

Can you give some skills so i can try doing it?

3. Re: Line Patterns by:hwangat 2007/12/12 22:49Your teacher did not tell me about those methods,either.
But your teacher told you that use the nextInt() method of java.util.Scanner
So I go to google ,type java.util.Scanner, and I found many pages.
For example: Scanning Text With java.util.Scanner and Covariant Return Types ...

How to draw the point: your teacher already provided you files. You need to read those files and find out how to do it.
If you still have problem after you have read it, you can post your question with what you have read.
But you need to read it first.

4. Re: Line Patterns by:Paraliasat 2008/01/03 20:20Hallo again and HAPPY NEW YEAR!! ;D ;D

I made some things with this project and you can see it in the attached file.

I've drawed the Ox , Oy lines,i've opened the file and have read all the point and also i've drawned them.

For each point [i]p[/i],i have made my program to find the points that are in the same line with [i]p[/i] if they have the same x or the same y.

Now,according to the description of my exercise i have to:

for each point [i]p[/i] i have to make my program to find the angle that each other point [i]q[/i] makes up with the p and sort all points according to the angle that they make up.

This must be done using the HeapSort in base with the MaxPQ.java
We must name this class PointHeapSort.java
This algorithm must use the compareTo method for objects with type of Point.
So,i must make a class Slope for couples <angle, Point> which is going to make the Comparable and the method compareTo will sort two couples according to th angle that they have.
The class PointHeapSort will contain only one method: void sort(List<Slope> l)

5. Re: Line Patterns by:hwangat 2008/01/04 10:39To find the angle between points you can use vector to calculate it.
First transfer angle between 3 points to angle between to vectors.
Assume you have 3 points: A(xa,ya), B(xb,yb), C(xc,yc)
And if you want to find angle between AB and AC
1. create two vector AB=(xb-xa,yb-ya), AC(xc-xa,yc-ya)
2.  inner product between two vectors AB AC =|AB|*|AC|*cos(cta)
You can cllculate cos(cta)= inner product of two vector/ (|AB|*|AC|) to find angle cta
where |AB| is the length of vector AB.

I think you know how to do the sorting.

6. Re: Line Patterns by:Paraliasat 2008/01/04 17:38I thought of calculating the angles with the Math.atan2 function.
For example:
if i have the points A(xa,ya), B(xb,yb), C(xc,yc) and i want to find angle between AB and AC
i will do for the AB: angle=(yb-ya , xb-xa) and for the AC: angle=(yc-ya , xc-xa)
And i will save each angle in a vector,so i can sort them later.
Is it wrong?

7. Re: Line Patterns by:hwangat 2008/01/04 21:11It is sound good to me.

8. Re: Line Patterns by:Paraliasat 2008/01/07 14:50Ok,now what is wrong with this code?

Why the compareTo is wrong?It returns 0 even for different angles.

Can you fix me the problem?

The whole code is in tha attched file.

9. Re: Line Patterns by:Paraliasat 2008/01/07 14:57I forgot to say that the programm should print: (16384, 32000) -> (16384, 18666)
for the input:
16384 19200
16384 18666
16384 32000
16384 21761
10000 10000

Pleasa Help me

10. Re: Line Patterns by:hwangat 2008/01/07 22:10I did download your zip file. However, there are 9 java files in the zip file.
I do not have time to read all those files (more then 40K of java code).

If you can write down the essential code. May be I can try to help you.
But, I really do not have time to read all your java code.
There is no way I can do this kind of things for everyone. Sorry!