Showing posts with label programimng. Show all posts
Showing posts with label programimng. Show all posts

Sunday, May 2, 2010

Gouraud Shading in JavaScript :-)

[Update I just changed demo to new version, now on my machine it reaches speed from 90 to 100-110 fps per second :-)]

I was wondering if it is even possible to create Gouraud Shading with JavaScript.

And answer is yes :-)

Here is a proof on youtube :-)



I was wondering also if it is possible to run it on G1 on Android 1.6, and answer is also yes:



As you can see on Chrome we have about 30-40 fps [when recording was of it was almost always near 50 fps on my laptop with Intel Core Duo 2 2.2 GHz on Chrome 5, 20-25 fps on Firefox], on G1 it was only 0.5 fps :-)

If you want to check how it will work on your machine you may try:




If you are reading this post by RSS or Buzz you may use direct link to demo :-)

It will work on Chrome, Chromium and Firefox. I don't know why it doesn't work on Opera :-(

Earlier versions of this worked about 5-10 times slower but it was caused by using fillRect(int,int,int,int) of Canvas object, now I'm working directly with pixels and this gives us big speed boost :-)

Similar postsbeta
Chrome OS and Java
Which language is fastest? ;-)
How to detect clickbaits?
Chrome2Chrome - lets move browser tabs between computers ;-)
OOo2GD - surveys results

Sunday, January 11, 2009

OpenOffice.org2GoogleDocs 1.4.1

New day and new version ;-) This time it is 1.4.1. In this version I fixed little and nasty problem which caused that in some rare occasions users got error Problem: no object DCH for MIME application /atom+XML instead of uploading files to Google Docs.

Problem was caused by the fact that in some cases Swing EDT thread might be started with "wrong" ClassLoader, and wasn't able to access libraries needed to contact with Google Docs. This version should fix this problem.

Download


Similar postsbeta
OOo2GD 1.5.0 - we can download spreadsheets :-)
OpenOffice.org2GoogleDocs 1.4.0
Chrome OS and Java
OOo2GD 2.1.1 is ready :-)
OpenOffice.org2GoogleDocs and Apple - first try ;-)

Sunday, July 6, 2008

Recursion is evil ;-)

Recursion is evil :-)

I'm playing with Google riddle from some code.jam event.

As a result of play I have function [here without any optimization and argument checking]:

public long findMaxF(long d, long b) {
long f = 0;
if (b==1) {
f=d;
} else if (b==d) {
f=(1<<d)-1; // 2^d - 1
} else {
f = findMaxF(d-1,b)+findMaxF(d-1,b-1)+1;
}
return f;
}

I have even less complicated version giving identical results:

public long findMaxF(long d, long b) {
long f = 0;
if (d<b) b=d;
if (b==1) {
f=d;
} else {
f = findMaxF(d-1,b)+findMaxF(d-1,b-1)+1;
}
return f;
}


I know that it is possible to calculate it without recursion... but I don't know how ;-) My only idea for now was to "simulate" some kind of stack ;-) with keeping list of directions how we moved on recursion tree, but I don't like this idea ;-) d and b may be really big, not simply thousands but also millions and billions [but if b is greater then 32 this method will return something bigger then 2^32 so more then 4 billions with something following]

My code works great with small set of data but totally fails with large :-) For now it fails because StackOverflowError :-) but why not if d is counted in millions ;-)
Even my idea with simulated stack will fails with such big values.......

I feel that it should be possible to not only avoid recursion [this is sure ;-)] but that this should be done in "linear" way, with 1 pass algorithm....

OK, I divide with you my sadness ;-) I feel better now ;-)

[Update: 09/07/2008]
Yesterday I decided that it cannot be in this way that I cannot do this ;-)
I decide to analyze this function. I thought in this way, first of all, if we will draw 3d chart of this function and as x we will use d, and as y we will use b, this function will have results only in are between y=1 and line described by y=x.
Additionally when y = 1, this function will return value d [so value of x], and on this line y=x [b=d] it will take value 2b-1. So I'm interested what kind of function describes value of this function with fixed b value....
I printed results for function for d=20 and b from 1 to 20....
I put it into spreadsheet and checked what if I will calculate f(n)-f(n-1).... :-)
I got some results, and those results was symmetrical... It was a clue ;-)
Difference between next values of function was described by binomial coefficient :-)
Binomial coefficient is described by:
n!/(k!(n-k)!)

So I needed to calculate factorial.... Thought by sad memories from fighting with recursion for original function I decided to use "linear" algorithm ;-) When I was using BigInteger it was working, and was able to calculate binomial coefficient for almost anything in finite time.... but this finite time might be counted in decades ;-)
I analyzed formula to calculate coefficient, changed code, this worked but still was slooooowwww.
Finally I found in Wikipedia article about binomial coefficient in programming languages. I used it, and it's working :-)

Similar postsbeta
How to create valid file name?
How we may use Google Earth Plugin? :-)
How to get negative number from size() in LinkedList in Java? ;-)
Abuse of Booleans ;-)
Which language is fastest? ;-)

Sunday, March 16, 2008

How many i 1+1 in Java? ;-)

Last time we spoke about immutability of String in Java, today lets talk about adding Integers ;-)

Lets check how many is 1+1 in Java...

For first try we will use this code:
public class Integers { 
public static void main(String[] args) throws Exception{
Integer i1 = 1;
Integer i2 = 1;
System.out.println(i1+"+"+i2+"="+(Integer)(i1+i2));
}
}

After compile and run we will see on the screen:

1+1=2

It was easy :-)

But to our next try we will add some additional code....

import java.lang.reflect.Field;
public class Integers {
public static void main(String[] args) throws Exception {
Integer i1 = 1;
Integer i2 = 1;
System.out.println(i1+"+"+i2+"="+(Integer)(i1+i2));
fixIntegers();
System.out.println(i1+"+"+i2+"="+(Integer)(i1+i2));

}

static void fixIntegers() throws Exception {
Class integerClass = Integer.class;
Field value = integerClass.getDeclaredField("value");
value.setAccessible(true);
value.setInt(Integer.valueOf(2), 3);
}

}


And now strange things happens....

After we compile and run we can see....

1+1=2
1+1=3

How it's possible? ;-)

Here short explanation ;-)
Method fixIntegers() changes value of int stored as "int representation" in some Integer... This some Integer is rather important ;-) In Sun Java was decided that conversion of some numbers [actually from -128 to 127] is rather often, so why to make this conversion so often? More reasonable will be to keep somewhere array of those Integers and when it will be needed use one of those objects. Method fixIntegers() first gets Integer object for int value of 2, and using reflections it changes value from 2 to 3 :-)
Next "magic" things are in line where we made second addition, we adds 1 to 1, and of course we got as a result 2, but we also wants to convert it to Integer, instead conversion we have here usage of value from "pool", we take this object :-) But because we wants to print String representation of this value method toString() of our object is called... and toString() looks to private variable value and finds in this place... 3 :-)
And it's whole secret ;-)

Similar postsbeta
Abuse of Booleans ;-)
The Secret Life of String ;-)
How to get negative number from size() in LinkedList in Java? ;-)
How to create valid file name?
Recursion is evil ;-)

Monday, January 28, 2008

OpenOffice.org2GoogleDocs 0.8 :-)

I had some spare time to finish coding of new features for AddOn ;-)
New in version 0.8 are:

  • new method of downloading of documents from Google Docs, this time without using of default web browser ;-) - author of downloading: Stefan Kolarov.
  • translation to Polish, German and Bulgarian. Authors of translation: Polish - me ;-), German - Pascal Herbert, Bulgarian - Stefan Kolarov.

    New version 0.8 you may find on OpenOffice.org extensions page.


    Similar postsbeta
    OpenOffice.org2GoogleDocs 0.8.1 - fixes :-)
    OOo2GD 2.0.0, now with autoupdate of documents in Google Docs :-)
    OpenOffice.org2GoogleDocs 1.0.1
    OOo2GD 1.8.1 - 2 fixes
    OpenOffice.org2GoogleDocs 0.9.1
  • Wednesday, January 9, 2008

    OpenOffice.org2GoogleDocs 0.7.1

    OpenOffice.org2GoogleDocs 0.7.1 is ready :-)
    This version should fix problem with exporting of ODS files (OpenOffice.org Calc Spreadsheets), also version information was added to extension.

    Rest of features is coded ;-)

    Newest version of AddOn you may find at project page.


    Similar postsbeta
    OpenOffice.org2GoogleDocs 0.8.1 - fixes :-)
    OpenOffice.org2GoogleDocs 0.6.5
    OpenOffice.org2GoogleDocs 1.2.0 - WebDAV support
    OpenOffice.org2GoogleDocs 0.7
    OpenOffice.org2GoogleDocs 1.2.1

    Saturday, December 15, 2007

    Why it isn't working???

    I got some reports from users of my OOo2GD extension. It looks that under Linux, and from this what I may see mainly under Ubuntu, Google Docs menu doesn't appear after installation of OOo2GD.
    In most of those systems helps resolution #1 from Known Issues published on project wiki. Also version 0.5 looks to work without problems.

    From this what I know only two files may be connected with this problem, first is Addons.xcu [file with description of extension, this is a place where OpenOffice.org looks to check what should put into menu, where to search code and which applications from pack should see this extension], second is one of classes called GDocs [it's first class loaded by OpenOffice.org, and in this class is coded dispatcher called by OpenOffice where it tries to show menu of extension, and when user use menu items of extension].

    Problem is that those two files are almost identical between version 0.5 and 0.6.
    In 0.6 I added to Addons.xcu line for narrowing set of applications in pack where AddOn should be visible. In GDocs I simply made small refactoring by moving direct using of literals to using of constants.........

    I made small experiment and copied Addons.xcu from version 0.5 to 0.6, but problem still occurs on my Ubuntu........ It looks that I must search harder :-)

    [why I wrote this? In my first company [Aksel or AKSEL-NET] we used to wrote monthly reports, it was boring and rather ineffective but sometimes it helps me to find solution of some problems, so I wrote it here to organize my thoughts :-)]


    Similar postsbeta
    Vacation in progress ;-)
    Early access version of OOo2GD 1.7.0 with partial MacOS support
    OpenOffice.org2GoogleDocs and Apple - first try ;-)
    OOo2GD 2.1.1 is ready :-)
    ePubGenerator v0.0.3 - from OpenOffice.org to EPUB :-)