### Author Topic: Line Patterns  (Read 14055 times)

#### Paralias

• Newbie
• Posts: 6
##### Line Patterns
« on: December 11, 2007, 04:36:48 pm »
Hallo everybody!

Newbie here

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

You can download all the files from here:http://eclass.aueb.gr/TMA212/document/%C5%F1%E3%E1%F3%DF%E5%F2/%C5%F1%E3%E1%F3%DF%E1%202/assignment2.zip
And some images here:

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Line Patterns
« Reply #1 on: December 11, 2007, 10:43:27 pm »
I 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.

#### Paralias

• Newbie
• Posts: 6
##### Re: Line Patterns
« Reply #2 on: December 12, 2007, 07:36:52 pm »

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?

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Line Patterns
« Reply #3 on: December 12, 2007, 10:49:14 pm »
Your 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.

#### Paralias

• Newbie
• Posts: 6
##### Re: Line Patterns
« Reply #4 on: January 03, 2008, 08:20:16 pm »
Hallo again and HAPPY NEW YEAR!!

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 p,i have made my program to find the points that are in the same line with p if they have the same x or the same y.

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

for each point p i have to make my program to find the angle that each other point q 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)

Can you please help me?I am desperate and i don't know what to do next.

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Line Patterns
« Reply #5 on: January 04, 2008, 10:39:14 am »
To 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.

#### Paralias

• Newbie
• Posts: 6
##### Re: Line Patterns
« Reply #6 on: January 04, 2008, 05:38:35 pm »
I 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?

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Line Patterns
« Reply #7 on: January 04, 2008, 09:11:45 pm »
It is sound good to me.

#### Paralias

• Newbie
• Posts: 6
##### Re: Line Patterns
« Reply #8 on: January 07, 2008, 02:50:47 pm »
Ok,now what is wrong with this code?

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

Can you please help me?Today i have to submit it.
Can you fix me the problem?

The whole code is in tha attched file.

#### Paralias

• Newbie
• Posts: 6
##### Re: Line Patterns
« Reply #9 on: January 07, 2008, 02:57:15 pm »
I 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