Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Algorithms?

  1. #1
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default Algorithms?

    I've sort of heard about algorithms before and it has something to do with lots of data. But I've never really understood what it meant. Could someone tell what an algorithm is, and possibly, how to make one?
    - Mike

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    An algorithm is just a way to solve a problem, especially mathematical. Thus, they take input and give output.

    For example, the algorithm to find the area of a circle from its radius is d = PI * r * r, where d (diameter) is the output and r (radius) is the input. The algorithm to find the pixel top of an HTML element on the page in most browsers is (based on code from QuirksMode.org):
    Code:
    	if (obj.offsetParent) {
    		curtop = obj.offsetTop;
    		while (obj = obj.offsetParent)
    			curtop += obj.offsetTop;
    	}
    ... where obj (object whose top to get) is the input and curtop (top in pixels of obj) is the output.
    Last edited by Twey; 09-04-2006 at 09:36 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    For some reason, I knew you would answer this. You talk about algorithms like ice-cream.
    - Mike

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    So what's the real point to algorithms?
    - Mike

  5. #5
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    To solve problems

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Quote Originally Posted by mburt
    You talk about algorithms like ice-cream.
    Lol! What's that supposed to mean?
    Quote Originally Posted by blm126
    [The point of algorithms is] To solve problems
    Precisely.

    Every time you write a script or program, the first thing you do -- consciously or not -- is to design an algorithm, a simple breakdown of the exact steps the computer needs to perform to accomplish the task. In fact, in some ways it could be said that a computer program is an algorithm.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Lol! What's that supposed to mean?
    It was a bad simile....
    - Mike

  8. #8
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    This may be the algorithm to find the bottom left pixel of an object.

    Code:
    if (obj.offsetParent) {
    		curtop = obj.offsetTop;
    		while (obj = obj.offsetParent)
    			curtop += obj.offsetTop+obj.offsetWidth;
    	}

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'm afraid it isn't.

    The algorithm above only expressed one dimension; to express two, you'd need an array or an object, which is where my getBoundingBox function comes in:
    Code:
    function getBoundingBox(obj) {
      var curleft = 0,
        oObj = obj;
      if (obj.offsetParent)
        while (obj.offsetParent) {
          curleft += obj.offsetLeft;
          obj = obj.offsetParent;
        }
      else if (obj.x) curleft += obj.x;
    
      obj = oObj;
    
      var curtop = 0;
      if (obj.offsetParent)
        while (obj.offsetParent) {
          curtop += obj.offsetTop;
          obj = obj.offsetParent;
        }
      else if (obj.y) curtop += obj.y;
    
      var curright = curleft + oObj.offsetWidth,
        curbottom = curtop + oObj.offsetHeight;
    
      return {'left' : curleft, 'top' : curtop, 'right' : curleft + oObj.offsetWidth, 'bottom' : curtop + oObj.offsetHeight};
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Great function Twey but it would be useful to me if you could tell me how to use it.

    And I thought Algorithms were all about solving a problem step wise something like this:

    Code:
    if two plus three equals 5
    show the user 5
    else
    simply show him you are wrong.
    Last edited by shachi; 09-05-2006 at 04:38 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •