Tag Archives: for beginners

How to format C and C++ code in Sublime Text

I am writing some C code on my mac using Sublime Text and I needed a quick way to format the code for better readability.

I found this package for Sublime Text named SublimeAStyleFormatter: https://packagecontrol.io/packages/SublimeAStyleFormatter

I’ve installed it with git by going on the packages folder and cloning the git repo:

cd ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/
git clone git://github.com/timonwong/SublimeAStyleFormatter.git

And that was it! I can format my C code by pressing Ctrl + Alt + F.

Postgres Query with GPS distance calculations without PostGIS

Let’s consider the following scenario:
You have a Postgres Database and an addresses table which stores items with GPS latitude and longitude values. You want to select all the items located inside a circular area around a GPS point you have.

You can write your SQL query without using PostGIS extension.
Let’s define the following data:

1. table: streets
id | name | latitude | longitude

2. GPS_CENTER_LATITUDE, GPS_CENTER_LONGITUDE = coordinates of the center area point around which you wanna search streets

3. AREARADIUS = the area’s radius expressed in meters

			(acos(sin(radians(s.latitude)) * sin(radians(GPS_CENTER_LATITUDE)) + cos(radians(s.latitude)) * cos(radians(GPS_CENTER_LATITUDE)) * cos(radians(s.longitude-GPS_CENTER_LONGITUDE))) * 6371 * 1000) computedDistance, 
			streets s 
as tempQuery 
	computedDistance < AREARADIUS;

Find if a point is inside a polygon with native PostgreSQL

There is the big and exhaustive PostGIS which can be installed into your PostgreSQL database and will provide you with lots of geometry functions and types you can use for your complicated geometry problems.

But what if you need, like I needed, to find out wether a point is inside a described polygon or not and you don’t want to go into all the trouble of installing PostGIS?

Well, good news. As I was happy to learn, PostgreSQL has native support for some geometry types and operators.

For instance, it supports the type polygon, which has the following syntax:

( (x1,y1) , (x2,y2), ... , (xn,yn))

It also supports the following operator which means “contains”


So, let’s put this two together and make a functional example:

-- postgresql demo for point-in-polygon with native support

create table test_polygon
("id" serial,
"data" polygon NOT NULL

insert into test_polygon (data) values ('((1,1),(12,1),(12,12),(1,12))');

-- example of query which will validate a point as being inside the polygon
select * from test_polygon where data @> '(3,4)';

-- example of query which will invalidate a point as being inside the polygon
select * from test_polygon where data @> '(24,14)';

The code is simple and powerful. If you combine it with an online tool for generating a list of coordinates for a polygon drew on a map, you can easily create systems which will compute if a GPS point is inside that map polygon.

As for PostgreSQL’s native support for geometry stuff, (which I believe is great), you can read more on the manual here: http://www.postgresql.org/docs/9.3/static/datatype-geometric.html and here: http://www.postgresql.org/docs/8.2/static/functions-geometry.html.


How to make NetBeans 8 for PHP look more like Notepad++

Writing on the same subject here as the last 2 articles (line-spacing and python support) on NetBeans.

Little back-story, for the past 7 years I worked and I’m still working all my web-related projects in Notepad++. I’ve tried all the IDEs out there, didn’t liked any of them for reasons more or less logical (Yes, sometimes I listen my feelings/emotions when making choices, for example “I like this editor or not” :p )

Recently I decided to give another try at NetBeans (following my colleagues persistence 😀 ) and first thing I wanted to do was to make it look more like my notepad++. Especially when it comes to color highlight and syntax.

I found the same question being asked here. One of the answers gave a full solution to the case: a download offering all the color-configs for making the NetBeans IDE show PHP codes colored like Notepad++ does.

You can download it from the following link: Notepad++ Theme for NetBeans 6.7.x

1. Unzip file
2. From menu go Tools/Options then click on Import button in the shown dialogue browse the unzipped folder
3. From the shown tree check “Fonts & Colors” and click on OK button.
4. After restarting IDE again from menu goto Tools/Options select Fonts & Colors tab and from Profile combo box select “Notepad_plusplus”
have a nice time.

Since I have no idea if the download link will work forever (probably not :p) or if the original poster will take it down, etc, etc, I am uploading the source on my blog as well and provide it for download from the link below. Again, all the credits go to the original author and to the user who posted the solution on the askubuntu.com forum: Saeed Zarinfam (I don’t know if the author of the config files is the same person with the author of the post, whomever they are, many thanks!)

Download directly: Notepad_plusplus_Theme_for_NetBeans_6.7.x_(php5-html-css)

How to increase line-height (line spacing) in NetBeans 8

Being a Notepad++ veteran user, the differences in code coloring, highlight and editor display in NetBeans IDE kinda put me off.

So first thing I wanted was to increase the line-height of the text (code, to be precise :p ) which is shown in NetBeans’ editor.

Luckily for me, I was not alone, other people asked the same question on google and some even answered it :))

The best answer by far is the one given by user pengemizt from stackoverflow.com on the question you can find here. That question was asking about NetBeans 7 but I just tried it on NetBeans 8 and it works ok.

I’m gonna copy/paste the answer here (all the credit going, like I’ve said above, to user pengemizt)

1. Find org-netbeans-modules-editor-settings-CustomPreferences.xml in ~/.netbeans/x.x/config/Editors/Preferences where x.x is your netbeans version.
In my case, this was C:\Users\Andrei\AppData\Roaming\NetBeans\8.0\config\Editors\Preferences\org-netbeans-modules-editor-settings-CustomPreferences.xml

NOTE.: By trial and error I found that this file is generated only after you first run NetBeans first time. I’ve just installed it on my home computer and decided to do all the changes first, and this file was nowhere to be found on my pc, so I had to run first netbeans and then edit the xml config file.

2. Add this xml markup just above </editor-preferences> or anywhere if you know what you doing.

    <entry javaType="java.lang.Float" name="line-height-correction" xml:space="preserve">




And I may add, restart NetBeans. The solution above gave as example setting the line-height to 1.4 but in my tests I found that a value of 1.2 works better (for me).

How to set up Netbeans with Python support

Recently I had to work with some python-powered codes and one of my colleagues pointed me to a nice tutorial on how to enable support for Python on Netbeans IDE.

Since I’m a 7-years-old Notepad++ veteran user :p I was glad to get some help on my new “still in tests” IDE of use 😀

So here’s the tutorial’s link. Enjoy!

BTW, although the tutorial is for Netbeans 7.1, I can confirm it works on NetBeans 8 as well.


Basic PHP PDO Extension Usage & Syntax

PDO Extension defines a consistent interface for accessing databases in PHP.

What does that mean? Well, it means programmers can write PHP codes which can be easily ported from one database type to another; let’s say you write your codes for MS SQL but then suddenly the project takes a turn and you need to adapt your codes for MySQL … would be great to be able to make the transition easy, wouldn’t be?

Now, you can read all about it on the Official PHP Manual and also you can read tutorials about it, but I’m about to give you below a simple example of the basic PDO Syntax.

$db = new PDO(
	'mysql:host = localhost; dbname=someDbName; charset=utf8', 

	$res = $db->query('err');
catch(PDOException $ex){
	echo "error";

//other code examples:
$stmt = $db->query('select * from table');
$results = $stmt-?fetchAll(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();

//here's how to get the ID of the last inserted row
//when you're doing an insert before
$lastId = $db->lastInsertId();

//preparing queries
$stmt = $db->prepare("select * from table where id=:id and name=:name");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

AJAX Classic Syntax

I’ve posted the jQuery Basic Syntax for making an AJAX call, but what about the classic javascript version?

So here it is, for beginners, the classic Javascript code for making an AJAX request; in the classic version you have to supply slightly different code for instantiate the request object, depending on if you’re on Internet Explorer or the rest of browsers.

Enjoy! 🙂

var xmlhttp;
	// code for FF
	xmlhttp = new XMLHttpRequest();
	// code for IE
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.open("POST", "some.php", true);
xmlhttp.setRequestHeader("Cotnent-type", "application/x-www-form-urlenconded");
xmlhttp.onreadystatechange = function(){
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200){

Basic jQuery AJAX syntax

Here’s another one for beginners.
The basic jQuery AJAX syntax you’ll use most often:

	type: "POST", // you can also use here "GET"
	url: "some-url.php",
	data: {name: "john", location: "boton"}
	alert("returned message: "+msg);
Page 1 of 212