Archive for January, 2016

My first try creating a bash script

January 30, 2016 2 comments

I am a ubuntu user, but sometimes I also use windows, mainly because my computer in the office is using windows. So, I use both ubuntu and windows. Even, my laptop has both windows and ubuntu. I tried to get rid of windows once, and installed wine to use windows application in my ubuntu. But it runs very slowly and it’s killing me and in the end, I came back to reinstall windows. I mainly use ubuntu for my research. The softwares I use for my research are auto, python, dstool, latex, xfig which run smoothly in ubuntu, even though python and latex can also be installed and run smoothly on windows machine. On the other hand, I use windows to do some regular activities such as browsing the internet, watching movies, checking my email, creating an office documents etc. All of which can be done in ubuntu as well. But there are activities that I must use windows, I sometimes need to use matlab and sometimes I like to play a game that only runs in windows. These two things are the main reason I still come back to use windows. Recently, I learn python so I am now trying to less use matlab.

That is just a background and my main point here is about bash scripting. After a few years using ubuntu, I have not created any bash script. Today, finally I learn to create one script. I created a script to automate my boring routine. When I write a paper, I need some illustrations. I mostly use xfig to create some mathematical images and to be able to use \LaTeX in the figure I need to convert it to an eps file. The produced eps file will be then converted to a pdf file as it is perfectly compatible to my pdflatex command. But before that, I need to crop the resulted pdf file in order to remove white space around the image. Suppose the name of my xfig file is spam.fig. I then write a series of command.

figtex2eps spam.fig
ps2pdf spam.eps
pdfcrop spam.pdf

I want to write a script that do all the above automatically. Thus I created the following script.

# Converts a .fig-file (xfig file) to a .eps-file by using a built-in function figtex2eps
# and then convert it to a .pdf file by using a built-in function ps2pdf
# and finally convert it to a cropped pdf file by using a built-in function pdfcrop
# ivanky saputra
# credit to :
# $ /home/abel/c/berland/etc/cvsrepository/figtex2eps/prog/figtex2eps $
# $ ps2pdf in ubuntu $
# $ pdfcrop in ubuntu $
echo "We are going to change the following $1.fig to a cropped pdf file"
function quit {
if [$1 == ""];
    echo "no files given";
    echo "Processing $1.fig............";
    figtex2eps $1.fig
    ps2pdf $1.eps
    pdfcrop $1.pdf
    echo "Done";

As someone has said that it is better to share your code and data openly as we as human are idiots and will make mistakes, please give me any suggestion to improve mine.

How to compute distance between a point and a line

January 27, 2016 Leave a comment

This semester (Even Semester, 2015-2016) I am teaching a new course, that is called Analytic Geometry or Geometry Analytic, both are the same, I think. Don’t get me wrong, the course is not new, it is just I would be teaching this course the first time. When they told me that I was going to be the lecturer of this course, they did not give me a standard, they did not give me a textbook they normally use, or a list of topics that I must cover, perhaps because this course is not a compulsory course. So, I have a freedom, I can choose topics that I want to teach in Analytic Geometry. The first thing I did, was to browse the internet any textbook about Analytic Geometry or any lecture note. In the end, I pick a textbook (Indonesian book) and I choose some topics of my interest to be the material of this course.

Okay, enough for the background, I am sure you don’t want to hear anymore on that. Let’s get back to main point of this note. Long story short, a straight line became one of the topics in my course, and I was interested on how the formula to compute distance between a point and a line is derived.In this case, I only consider two dimensional case and by distance, I mean the shortest distance between such a point and a certain line, which also means the perpendicular distance. If you don’t remember what is the formula, let me recall you.

Consider a straight line l, given by the following equation:

l : ax + by + c=0.

Suppose we have a point P(x_p,y_p), then the distance between the point P and the straight line l is

d = \frac{|ax_p+by_p+c|}{\sqrt{a^2+b^2}}.fig1-crop

There are other proofs on how to derive the above formula, but I really really like the proof I am going to show you below.  I will divide this note into two section. The first section will talk about a straight line and how to get an equation of a straight line. The latter section will derive the formula.

1. A straight line equation

In high school, I think you must know what conditions we need to have in order to obtain a straight line equation. If we have two points in XY-coordinate we can compute the line equation using the following:


If we have a gradient and a point, we can also compute the line equation using the following:


Of course, in a problem, we won’t get this information so easily. We have work a bit more to get either two points or one point and a gradient and then we are able to obtain the equation of the straight line.

Before I teach this course, I can only conclude that if you want to know the equation of a straight line you need to know either:
(i) two points, or
(ii) a point and gradient of the line.
But, actually there is a third condition, and if we know this condition, we can also compute the equation of a straight line. In the third condition, a line is assumed to be the tangent line of a certain circle. Thus, the characteristics we need to know to form a line are the radius of the circle and the angle between the radius and the positif x-axis. See the figure below.fig2-crop

In the figure, we can see that a line can be determined uniquely if the radius of the circle n and the angle \alpha are known. In the first section of this note, we shall derive how to define a line equation given these two conditions.
Suppose we have a straight line, n are \alpha are given. See the figure below. Consider the point P(x,y). We are going to find the relationship of x and yfig3-crop

The radius n, which is |ON|,  can be computed by adding |OM| and |MN|. We shall consider |OM| first. Consider triangle OMQ which is a right triangle at M. We have the relationship:

|OM|=|OQ|\cos \alpha=x \cos \alpha.

Consider another triangle, PM'Q, which is also a right triangle at M'. We have the following relationship

|PM'|=|PQ|\sin \alpha=y \sin \alpha as it can be computed that the angle \angle PQM is also \alpha.

Therefore, we have n=x \cos \alpha + y \sin \alpha, which is the equation of straight line given that n and \alpha is known.

Before we discuss how to derive the formula to compute the distance between a point and a line, I would like to discuss how to find n and \alpha if we know the general equation of a straight line ax+by+c=0. Consider a line equationax+by+c=0, then we move $c$ to the right hand side and multiple both sides with a non zero constant kkax+kby=-kc. We shall choose k such that ka=\cos \alpha and kb=\sin \alpha. Therefore, we have

k = \pm \frac{1}{\sqrt{a^2+b^2}} and

our equation of line becomes:


Here, we have that the right hand side of the above equation is n=\pm(-\frac{c}{\sqrt{a^2+b^2}}), and we choose the positive value to get n. The angle \alpha can also be computed once we determine n.

2. Distance between a point and a line

To compute a perpendicular distance between a point and a line, we shall use the above result. Consider a straight line l:x \cos \alpha+y \sin \alpha = n_1 and a point P in the following figure.fig4-crop


We want to compute d. In order to do that, we need to make another line that is parallel to the line l and passing through point P. This line, because it is parallel to l and, has the following equation:

x \cos \alpha+y \sin \alpha = n_2

Thus the distance between the point P and the line l can be easily computed by considering the absolute difference between n_1 and n_2 as follow:

d = |n_2-n_1|=|x_p \cos \alpha + y_p \sin \alpha - n_1|

As we know from the first section that we can substitute the latter expression into:

d = |x_p \frac{a}{\sqrt{a^2+b^2}}+ y_p\frac{b}{\sqrt{a^2+b^2}}-(-\frac{c}{\sqrt{a^2+b^2}} )|



which is the same as the formula we mentioned in the beginning of our note.

Categories: Teachings Tags: , ,