Graph captcha test

by flaiming

HTML

<canvas width="400" height="400" id="cv"></canvas>
<input type="button" value="Validate" id="validate_button" />

JavaScript

var RADIUS = 10;
var AREA_SIZE = {width: 400, height: 400};
var CIRCLE_COUNT = 5;
var LINE_TEXT = false;
var CIRCLE_TEXT = true;

var ctx = $('#cv').get(0).getContext('2d');

function lineIntersection(a1, a2, b1, b2) {
    //is case of equal points return false
    if (a1 == b1 || a1 == b2 || a2 == b1 || a2 == b2)
        return false;
    
    var ua_t = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x);
    var ub_t = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x);
    var u_b  = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);

    if ( u_b != 0 ) {
        var ua = ua_t / u_b;
        var ub = ub_t / u_b;

        if ( 0 <= ua && ua <= 1 && 0 <= ub && ub <= 1 ) {
            return true;
            //result.points.push(
            //    new Point2D(
            //        a1.x + ua * (a2.x - a1.x),
            //        a1.y + ua * (a2.y - a1.y)
            //    )
            //);
        } else {
            return false;        
        }
    } else {
        if ( ua_t == 0 || ub_t == 0 ) {
            //shodne
            return true;
        } else {
            //rovnobezne            
            return false;
        }
    }
};

function inCircle(x, y, circle, radius) {
    
    var dx = x- circle.x,
    dy = y- circle.y;

    //see if the distance between the click is less than radius
    if( dx * dx + dy * dy < radius * radius  ){
        return true;
    }
    return false;
}

///////////////////
var Circle = function(name, radius, x, y) {
    this.name = name;
    this.r = radius;
    this.x = x;
    this.y = y;
    this.lines = [];
    
    var instance = this;
    
    this.toString = function() {
        return instance.name + '('+this.x+', '+this.y+')';        
    }
    
    //cycle through connected lines and returns true, if this circle is connected to circle c
    this.hasConnectedCircle = function(c) {
        for (var i = 0; i < instance.lines.length; i++) {  
            if (instance.lines[i].c1...