Latest Posts

Posted by Alex on 17th August 2010

Rotating Rectangles

Working on the maps48 project, one of the problems that needed solving was calculating the amount of map space needed to be retrieved to allow rotating the map without introducing any blank space in the viewport. As you can see from the diagram below what we actually need is to get the rotated rectangle’s horizontal and vertical projection as the new width and height.

The black border represents the viewport. If we were to only have an image the size of the viewport and rotate it, we’d end up with something like the orange rectangle. Notice the blank spaces in the viewport :). The solution is to resize (or import a larger bit of the map, in our case) the image – the pink rectangle – so that when rotated it fills up the viewport entirely, as does the green rectangle.

Problem description

This can be easily achieved through some basic trigonometry. The key to this are two angles: alpha, the angle the rectangle was rotated by and beta, the angle the diagonal makes with the width.

Problem diagram

First,  I determine the angle the diagonal makes with the baseline (the rectangle’s width):

Equation 1

Now, we know that:

Equation 2

This way we can determine the new height. The new width can be determined in the same way:

Equation 3And here’s a translation of this in JavaScript:

function getRotatedDimensions(width, height, rot) {
	// rotation angle in radians
	var alpha = rot * Math.PI / 180;
	// the angle made by the diagonal and the width in radians
	var beta = Math.atan(height / width);
	// diagonal length
	var diagonal = Math.sqrt(width * width + height * height);

	var newHeight = Math.sin(alpha + beta) * diagonal;
	var newWidth = Math.cos(beta - alpha) * diagonal;
	return { "width": newWidth, "height": newHeight };
}

This was inspired by Adrian‘s post about overlapping rectangles. The graphics above were generated with Photoshop and the formulas were built using the excellent CodeCogs online Equation Editor.

Posted by Alex on 15th August 2010

New Talks: Freedom of Speech and Chosing

This week’s talks both come from TED.

Wikileaks founder, Julian Assange, gives an interview at TED Global 2010. This is someone that’s truly “making an impact”, maybe people should have this in mind before saying that they’re changing the world with their products :).

The second talk features Sheena Iyengar and focuses on choosing: how cultural differences affect the way we choose (or don’t choose).

Posted by Alex on 12th August 2010

Weblogic Encoding Issue

Trying to deploy some RSS feeds as .jspx views on a WebLogic 10 server, I noticed that it mangled all UTF-8 output. This was part of a Spring MVC web-application. The problem was that on my local development server (Apache Tomcat 6.0) everything rendered fine, but on the WebLogic server all non-ANSI characters were not outputted correctly.

In Firefox, I saw something like:

<summary>Formaci�n</summary>

The byte sequence for the strange character was 0xEF 0xBF 0xBD and I seemed to get that for all UTF-8 chars that I was supposed to receive in the tests I was conducting (á, ó, í). I checked the content-type and encoding in Firebug and it seemed ok (Content-Type: application/xhtml+xml; charset=UTF-8).

I later found out that � is the Unicode Replacement Character U+FFFD and that the problem was probably caused by the fact that the server, although told to output UTF-8, sent out ISO-8859-1.

The fix came from my .jspx files, more specifically the page directive tag. What surprised me was the fact that the order of attributes in the .jspx page directive matters! Initially I had this:

<jsp:directive.page pageEncoding="utf-8" contentType="application/xhtml+xml" />

This doesn’t work, because you also need to specify the charset in the contentType attribute:

<jsp:directive.page contentType="application/xhtml+xml; charset=UTF-8" pageEncoding="UTF-8" />

The above line works and determines the correct encoding. But, to my surprise, if you switch the order of attributes it doesn’t work:

<jsp:directive.page pageEncoding="UTF-8" contentType="application/xhtml+xml; charset=UTF-8" />

I don’t know where this is coming from, but, in my opinion it’s a bug in WebLogic. I’ll open a bug-report.

You can also find details about this issue on my Stackoverflow post. Thanks to BalusC for the help.

Posted by Alex on 30th July 2010

Huawei and the VPN Saga

After a horrendous experience, usually I feel the need to share my findings with the world. Other unhappy souls shouldn’t go through what I’ve gone through yesterday.

My company offers the possibility of working remotely, by connecting to its VPN. I have a USB dongle with my certificates and also a preinstalled software that manages the VPN connection. I travel fairly often to my hometown, Braşov. This week was no exception, I decided to work remotely for a couple of days.

In Bucharest I have no issues connecting to the VPN from home, I’ve done it quite a few times. The first surprise came to me when I realized that, although I have the same ISP (Clicknet, Romanian DSL provider), in Braşov my VPN connection failed. Moreover, when I tried to connect to it, I’d lose internet connectivity for about a minute. I first thought it’s a port forwarding issue, although it seemed strange that an ISP would have different port forwarding policies in different cities. Also, I couldn’t explain the connection loss. After being on the phone with “technical” support for about an hour, passed around by 4 or 5 people, trying to explain to most of them what VPN means, I finally got a straight answer from one of them: they aren’t blocking any ports, anywhere. I took a closer look at the modem/router’s behavior and then I noticed something Whenever I tried to connect to the VPN, my router would reset (!) itself. I have a Huawei SmartAX MT882 ADSL terminal, supplied by the ISP. In Bucharest I have a Linksys wireless router with ADSL support bundled.

I tried googling the problem for a while but finally I gave in and decided to buy a 3G modem, I was considering this option for some time now. I went to the Vodafone store and got a mobile internet subscription. I received a Huawei K3765 HSPA USB Stick; I went home and installed it. The installation process was extremely smooth; the stick already has its software bundled in it and it’s almost auto-installing. In a couple of minutes I was connected to the VPN and everything was working fine.

After about six hours of continuous connectivity, the 3G modem decided to stop working, right before I was trying to do a server push. I thought that it had overheated or something (it was extremely hot), so I restarted my PC and left the modem to cool down for a few minutes. When I plugged it back in, Windows failed to recognize it, as did the Mobile Connect application. I spent the next couple of hours trying to sort this out. I uninstalled the Mobile Connect Software, removed the USB entries for the modem from the registry, removed it from the Windows modem list and from device manager, waiting for Windows to re-detect it. Guess what, it didn’t.

Googling wasn’t much help, most of the questions we’re on Vodafone forums and the their quality (as the quality of the responses) was poor to say the least. Also, I found that in some situations, the Vodafone experts’ policy is to “Hey , I’ve sent you a PM regarding this issue”. WTF, you idiots! Other people have problems too and we’re supposed to be living the age of ubiquitous information, not sending private messages on crappy forums.

I then spent an enchanting half-hour on hold with Vodafone support. I just wanted to see if it’s a common issue for the modems to stop working, I was hoping the issue was not coming from my computer. Support was useless, no surprise there, so I decided to go to a Vodafone shop so they could test my modem and eventually replace it with a working one. Sadly, I discovered that the issue was coming from my computer. They tried my modem on one of the laptops they had on display and it worked fine. I decided to go over to a friend’s house to finish the updates I needed to finish. I also tested the modem on his computer and it also worked.

When I came back home, later in the evening, I started thinking about what could be causing the problem, but I couldn’t come up with any solution. My work-supplied laptop is running Windows XP and is bundled with tons of ‘enterprise software’ that make it run slower than PC’s made in the early 2000′s. Really. I’m not joking. I was so angry that I decided to purge my system of anything that I didn’t need, regardless if it’s “policy” or whatever.

I started with the anti-virus software, McAfee enterprise suite. I never understood why anyone would need anti-virus software, assuming he’s at least a bit computer savvy and doesn’t download warez daily. It turns out that uninstalling McAfee is an adventure. I had to remove about three or four of their products from the Windows Add/Remove programs list. I couldn’t remove the McAfee Agent, because it threw an “in-use” error when I tried to do that. If I restarted the system, the agent would reinstall what I’d previously removed. Swell. Eventually, I found out that the solution is to remove the McAfee common framework. It’s located in Program Files/McAfee/Common Framework and you need to run: frminst.exe /forceuninstall. After removing it, I ran their Consumer Products Removal tool and after a reboot I was McAfee free.

After removing the anti-virus, my 3G modem started working again. Damn.

So, “I’ve learned something today” aka conclusions:

  • Huawei, el-cheapo of networking devices, has a long way to go.
  • McAffee is a piece of crap. I suspect all antivirus software is.
  • I really, truly, hate Windows, Microsoft and the people that use the term ‘enterprise’ to hide incompetence.
  • Technical support is mostly useless for advanced issues, so why pay for it? (read the last point again, too)
  • I want a Mac :).
Posted by Alex on 14th July 2010

Two RailsConf 2010 Talks

Recently, I’ve started watching videos from famous software development conferences. I’m finding them to be a great source for both inspiration for programming but also excellent examples of what it means to give a good talk. I’ll try to feature one or two every week, because this way I’ll have an incentive to be consistent and keep watching.

This week, I’ve found two videos from RailsConf 2010 that I really enjoyed. The first one is by Derek Sivers, a former guitarist turned web entrepreneur and programmer (!) that talks about differences in everything, from people to programming languages. I really like this guy’s view of the World and the loads of common sense he brings to this business. Also, he’s a great speaker and his talk was very entertaining:

The second talk features Yehuda Katz, core contributor to the Rails framework, book author and creator of Visual JQuery. The talk is about open-source development and the barriers most developers face when trying to get involved in projects. Worth noticing is the repeated mention of George Danzig, famous mathematician an computer scientists, who, claims the speaker, proves that nothing is impossible if you’re willing to put in the effort.

Posted by Alex on 11th June 2010

Settting up the Eclipse environment for Spring

Currently, a project I’m working on is using the Spring Framework for web development. Since I’m new to Spring (and to Java in general), I’m trying to get the most out of the tools I have available. One of these tools is my IDE, Eclipse; I want it to show me the javadoc for Spring classes.

The project already had some of the Spring libraries on the build path, so I only had to download the documentation from the SpringSource site. After that, you need to go to your project’s properties, go to Java Build Path -> Libraries and for every Spring library that is referenced in the project point the documentation to the root folder of the downloaded documentation (javadoc-api folder). After this, the javadocs should appear as tooltips when you hover over Spring classes/methods.

Libraries for Spring documentation

Also, you should consider installing the Spring IDE Eclipse Plugin. Currently, their site (springide.org) is down and the advertised update site  is down as well. After some searching I came across this guide that got me through the installation process (which is really flawed).

I’ll update this post when I get the hang of using the plugin.