Monday, September 30, 2013

quick hit: minimal war building with ant

Sometimes I want a .war file but just with a few static files. While I have been known to use the hack of "zip the folder, rename it .war", that really requires a few extra files be inserted by hand into the folder (like META-INF etc) to do properly. Since I have ant on my system, I should use that.

If I have a folder of files, I just add this build.xml, and then typing ant makes me my "mything" war...

<project name="mything" default="build">
        <target name="build">
                <delete file="mything.war"/>

                <war destfile="mything.war" needxmlfile="false">
                        <fileset dir=".">
                                <exclude name="build.xml"/>
                                <exclude name="mything.war"/>
                        </fileset>
                </war>
        </target>
</project>

That's about as simple as it can get and still be doing the right thing, I think.

Thursday, September 26, 2013

seeker and you shall finder

Something I had to look up "how did I do that again?" in my little toy/gamemaking: have an object that will gradually turn to a target.

I made a really pretty version of this in an art game called aRTSeroids which I cribbed for this entry...

It's easy enough to get a seeker to snap instantly to its target, just do atan2(deltaY,deltaX); -- figuring out "which direction should I turn to get my angle closer to it" is a little tricker.



You can see the source here. The seeker object has an x and y propery for its position on the screen, and then an a property with its current angle. It also has a MAXTURN that sets how far it can turn per click...

  void seek(float rtx, float rty) { 
    float dx = (rtx - x);
    float dy = ( rty - y);
    //this trick seems to work to deal with the overlap..
    //they consider the shortest path even if other side of screen
    if (abs(dx) > width/2) {
      dx *= -1.0;
    }
    if (abs(dy) > height/2) {
      dy *= -1.0;
    }


    float wantangle = atan2(dy, dx);
    float anglediff = (a - wantangle);  
    anglediff /= PI; //I prefer thinking in multiples of PI...
    //this next bit catches the "runaround"
    if (anglediff > 1) {
      anglediff -= 2;
    }
    if (anglediff < -1) {
      anglediff += 2;
    }
    if (abs(anglediff) > .1) {  //arbitrary value to ignore
       if (anglediff > 0) {
        a -= smaller(abs(anglediff*PI),MAXTURN);
      } else {
        a += smaller(abs(anglediff*PI),MAXTURN);;
      } //could just be MAXTURN if you don't mind oversteer
    }
  }
}
float smaller(float a, float b){
 if(a < b) return a;
return b; 
}  

The first trick is the "deal with the overlap". This is optional, but nice for games that feature screen wraparound-- (i.e. fly left, show up right)-- if you want to have definitive borders you can leave those lines out.

The next bit is just the hack I got to normalize the difference in the angle... esentially we try to make sure the difference between the angle it "wants" and where it is now is normalized between PI and -PI. 

Finally, that smaller() code says it will either go MAXTURN or the difference in the angle. If it just went MAXTURN, it sometimes will do a little vibrate pattern as it overshoots the angle. (Come to think of it, I think that vibration is why I did that "arbitrary value to ignore")

Anyway, the code is a little clunky and weird but it works well, and should be useful to me in future projects.

quick and dirty iOS-web demos

If you're more experienced with html5 than xcode, or if you have another project need that means a mobile app is going to be primarily driven by a web view, http://conecode.com/news/2011/05/ios-tutorial-creating-a-web-view-uiwebview/ is very good.

Also, on the actual webpage, I built assuming for a width of 320px and had the following meta tags:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name = "format-detection" content = "telephone=no"/>
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
<meta name="apple-mobile-web-app-capable" content="yes" />
So that's limited because it only works in iPhone portrait mode, but still, for a quick and dirty demo, not bad.

(For a quicker and dirtier demo of the idea: just add the webpage to the homescreen. When they start it up it will be in fullscreen, no-safari-random-widgets, and to the unaided eye looks about the same, except the icon will be a screenshot rather than something you can set.)

UPDATE: My buddy Jonathan Zaleski says 
You can control the home-screen icon w/ the following:

<link href="favicon.ico" rel="apple-touch-icon">

Good to know!

Wednesday, September 25, 2013

color schemes!

If you design things, and don't know a ton about color theory, you should know this exists:
Color Scheme Designer.

For example, I was given a logo, and was able to mockup some more attractive webpages by grabbing the hexcode for its background color and using one of the schemes from it. There's a science towards knowing what colors have compatible "feels", and then an art to making it all work together.

Monday, September 23, 2013

quick and dirty "what will it look like in an iphone" mockup

<div style="background-image:url(iphone.png); width:396px; height:745px; position:relative;">
    <iframe style="width:320px; height:460px;  position:absolute; left:38px;top:145px; border:none;" src="http://kirk.is/"></iframe>    
</div>

with this image:

The one problem is scrollbars will be visible. I mess around with scrolling="no", but that eliminates keyboard and mousewheel scrolling. I tried some other tricks but nothing really jelled.

(See also: http://www.responsinator.com/ )

Tuesday, September 17, 2013

two men enter, one man leaves (blast from the past)

(Just found something I wrote in 2004...) You know, I just thought of one major lack of Java, and to be fair, most C-derived programming languages: a function can have many input parameters but only one return value. That's a really odd asymmetry to have to put up with.

Perl, for example, handles it much better...it's no problem to write something like 

($foo, $bar, $baz) = somefunction($a,$b,$c);

In Java, though, you'd have two ugly workarounds: create a wrapper object that contains each thing you want to return, or if the objects are complex, sometimes you can have the calling program create an object, pass it in, and let the called function fill in the blanks. (Kind of like Oracle's "INOUT" parameters.) 

(Followup: since I wrote this in 2004, JSON has gained in popularity and influenced other languages too, so that returning or passing around a Map of key/value pairs is probably more acceptable than it used to be.)

Personally, I think this lack is something that provokes over use of Exceptions, which really do horrendous things to understanding a programs flow of execution. 

Seriously, would it be so hard to add a syntax so something like this would work?

int foo;
String bar;
(foo,bar,String baz) = somefunction();

public int,String,String somefunction(){
   return (5,"hey","ho");


Does anyone know if any of the other C-derived languages handle this case better? C# or any of that? I know they're hyper-conservative about adding this kind of structure to Java, since it breaks old compilers and what not. But still, it seems like one of those things that actually is pretty stupid and only around for legacy reasons but that everyone just kind of accepts. 

Thursday, September 12, 2013

gowen's interesting tip calculations

A program to calculate tips is classic, boring stuff, but my friend gowen has an interesting take on it: he requested an app that could help figure out the tip values that would result in "interesting" totals, like "43.21" or "33.33" or what have you.

tl;dr: see the bookmarklet-able result here.

I decided that I'd write a bookmarklet in Javascript as a prototype... this would allow for easy modifications and tweakings while still loadable on a portable device without going through an app store or any other flimflam. (Plus I'm a little lazy.)

I knew one basic rule: DO NO TRY TO DEAL WITH MONEY VALUES IN A FLOAT. Rounding errors are very likely to bite.

I had to throw away my first attempts at juggling currency because they were too complicated. I tried to store "dollars" and "cents" as separate parts of the same object, and then do all the math based on that... but stuff like negatives were going to get funky, and it was a pain in the butt.

A much easier system is to just store everything as cents and then translate to and from traditional "DDD.CC" notation.

So, with basic money wrangling down, I built an "interestingness detector" that would return what way a number was "interesting", or undefined if not. Here's what I decided was "interesting":
  • multiple of ten e.g. 40.00
  • digit repeating e.g. 44.44
  • ascending e.g. 45.67
  • descending e.g. 43.21
  • palindrome e.g. 43.34
  • even amount e.g. 41.00
  • repeated halves e.g. 43.43
Once I had most of that done, I needed a loop to generate values that I could test.

My first pass started from a minimum tip percentage and worked its way a penny at a time to a maximum tip percentage, printing out each one that met the criteria for interestingness. This generated too many values. I tweaked my loop so that it would only show the "first" interesting result for each type (i.e. one "even amount" value, one "palindrome" value, etc) but this was biased towards low tip amounts. I then made a loop that started with a base tip value, and then tried one cent more AND one cent less, then two cents more and two cents less, etc, and printed the first value for each interestness-type going either way, which keeps things closer to the target tip percentage.

So, the result was like this

Pretty geeky, huh?

Once that was set I made a bookmarket. At it's heart it's just wrapping the code in this block:
javascript:(function(){
//code goes here
})();
this creates a "self-executing block", and any variables I made are scoped inside of it, and so won't interfere with the current page when the bookmarklet is called. I had that whole block as a standalone js file and provided instructions to copy and paste that as the URL of a bookmark. I also embeded it in the href="" of a link for more immediate satisfaction. 

There might be a less clunky way of letting people make a bookmarklet, and I can't see a way of putting a link directly to the iOS homepage, say, like you can with normal sites, but this should do until I decide to make a real app.

Again, you can see the end result at alienbill.com.

Now updated to recognize PI as an interesting value, as well as sorting the results by generosity, and putting the numeric values into something like columns for readability...