Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Friday, June 24, 2011

posting source code with styling and syntax color on blogger blogspot

Finally gotten around to looking up how to format source code nicely in blogs. ref: http://www.southernspeakers.net/2010/12/adding-syntaxhighlighter-to-blogger.html
Test code:
main() {
  int i = 123;
  printf("tst test";)
}
I've adjusted the instructions to use the new in version 3 feature of dynamic / auto loading of syntax descriptor files. To set up blogger / blogspot to show source code syntax highlighting and line numbering, open Dashboard, Design. Edit HTML, add the following code before "</head>"
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCoreMDUltra.css' rel='stylesheet' type='text/css'/>
<!-- other themes:
shThemeDefault.css
shCore.css
-->

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js' type='text/javascript'/>

<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.defaults['auto-links'] = false; 
</script>
And after the section:
<b:section class='main' id='main' showaddelement='no'>
<b:widget id='Blog1' locked='true' title='Blog Posts' type='Blog'/>
</b:section>
Add the code:
<script type='text/javascript'>
SyntaxHighlighter.autoloader(
 'py python http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js',
  'java    http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js',
  'js    http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJs.js',
  'css    http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js',
  'text plain http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js',
  'xml    http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js',
  'cpp Cpp     http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js'
);
SyntaxHighlighter.all()
</script>

To use, wrap your CODE with, <pre class="brush:xml">CODE</pre>, for xml and html. For other languages, replace xml with the language type, such as: cpp, java, js, plain.

You'll also need to html encode the text; two web hosted html encoders: http://www.string-functions.com/htmldecode.aspx http://www.elliotswan.com/postable/ (no adverts, but only does encoding, no decoding)

Saturday, May 28, 2011

basic rectangle intersection algorithm

I was sifting through LWUIT sourcecode, and came upon a function for determining if 2 rectangular areas intersect at any region. I stared for a painfully long time at the simple task and simple algorithm to figure out why it worked. It works by comparing the lower right and top left point of each rectangle. For there to be an intersection by rectangles r1 and r2, the top left of r1 is inside the quadrant described by the lower right of r2, and vice versa. If either of these is not true, it means r1 lies entirely outside a quadrant described by one of the corners of r2, which means the rectangles do not intersect ie, they are disjoint.
# intersect.py
# check if rectangles intersect
# rectangles defined by
# (tx,ty, tw=width, th=height)
# (x,y, width, height)

def intersects(tx, ty, tw, th, x, y, width, height):
        rw = width
        rh = height

        if (rw <= 0 or rh <= 0 or tw <= 0 or th <= 0): 
            return false
        
        rx = x
        ry = y
        rw += rx
        rh += ry
        tw += tx
        th += ty

        # The first sub-test checks if rw/rh/tw/th overflowed, meaning wrapped around to value < rx/ry/tw/ry
        return ((rw < rx or rw > tx) and
                (rh < ry or rh > ty) and
                (tw < tx or tw > rx) and
                (th < ty or th > ry))

def main():
 print(intersects(1,1,4,5,3,3,1,1))
 print(intersects(3,3,1,1,1,1,4,5))

main()