Thursday, February 18, 2016

w6d4 - Javascript, jQuery and blackjack

I woke up at 4 or 5 this morning and lay in the dark thinking of stabby rainbow triangles, as one does.  And, as often happens, a solution to my drawing problems came to me.

The problem lay in drawing n triangles arranged such that each triangle had the same base (width) but an arbitrary height corresponding to temperature.  The triangles should touch only at the base corners.  In looking at the <canvas> documentation, my initial impression was that rotate or transform would rotate or move a shape (it's the shapes that move in MS Paint or other simple drawing programs I've used.)  Trying to rotate the images last night met with miserable failure.

My o'dark-hundred realization was that it's the canvas that moving, not the image.  The other realization was that if I defined a new "center" for the canvas (using context.translate), then started drawing my triangle a suitable distance "above" the center, then rotate the canvas by a suitable portion of a circle, the triangles would all be tangent at the corners...by magic!  Or math.

(image from draw.io)

The "base" is the base width of the triangles (which I can specify).  The circle represents the center of the canvas as defined by me.  The arrow represents the "offset" distance - the distance "above" the center where the triangle should be drawn.  It's found using the tangent of angle A, where the opposite side is length base/2.  

My next realization is that I could draw a stellate polygon of n sides by setting A = 180/n and the offset as base/(2*tan(A)).  The context rotation between each triangle would be 360/n...or rather 2*PI/n since javascript loves radians.

My code right now is a god-awful spaghetti mess, so I'm not going to put it up yet.  I really need to improve on writing long programs in neat packets.  I got pretty good with python, but javascript has knocked me back into my reflexive write-everything-in-one-big-blob mode.

For now, I'll just say that I thoroughly enjoyed this once I figured out what I was doing.   It feels so good to stretch my math muscles again, however small the stretch may be.

I still would like to make the output more interactive, so the stars rotate when clicked, and display an image of the day's weather.  I know how I'd like to do it (just redraw the stars starting at one place further along the array).

The other project for the day was expanding a javascript version of blackjack that one of the instructors had started.  The base version had a rudimentary game engine, and the cards and deck defined (with images).  There was a game clock that monitored the state and redrew cards and printed text as needed.  

Reading expert code may be my greatest joy here, other than my own occasional breakthroughs.  It's so interesting to see how people break a problem into pieces and make them work.

The really fun part was unravelling what was going on since there was no documentation provided.  I'm happy to say that by the end of the day (about 4 hours), I'd managed to implement a scoring system, and add hit and stand capability.  I can see the way to finish the project.  

I have so much to learn still.  I'm definitely at the "OMFG" stage of learning, or the "desert of despair" from the Viking Code School blog. I'm at the point where I'd felt like I kind of knew what was going on with python...and now I'm realizing just how deep the well is, with sql, django, javascript, ajax, server management...it's dispiriting. It's not that I don't want to learn all that (I love learning, after all).  It's more that there's just so much before I can do anything useful.

Unless you count n-sided rainbow stellate polygons as useful, of course. :)







Hah!

Wednesday, February 17, 2016

w6d3 - HTML5, ajax, APIs and jQuery, or: How I spent 3 hours drawing a triangle

Today was jQuery basics and a revisitation of the openweather.com api.  The challenge was, starting with a simple form (that we built), get a city and the number of days of forecast desired.  Request the relevant info from openweather via ajax query and parse the resulting jQuery object (as opposed to the python/json parsing from our previous encounter).  Then, display the data in table.

The extension project was to display the data in a bar graph using the HTML5 <canvas> tag.  I was able to get both working after some initial faffing about with forms and data retrieval.  The bulk of my time was spent making a) a rainbow fill for a drawing object (not that hard), b) trying to draw triangles instead of rectangles (also not too hard) and c) trying to rearrange the triangles into a stellate pentagon (i.e. a star) which is far more difficult that I could manage.  It's apparantly weird enough that not even StackOverflow has a solution.  I have an idea of how to approach it, but it involves using the matrix transformation in HTML5...and it's been 20 years since linear algebra.  I can even move the triangle using the transformation, but I don't sufficiently understand how the context and positioning work such that I can change locations systematically

Here's my rainbow triangle, anyway:

And here's the Dallas (temperature) forecast for the next 5 days:


You can try it for yourself if you scroll all the way to the bottom of the post.  WARNING: You must type the city with capital letters in the proper place: New York, Columbus, San Francisco, Berlin.

Also, you should refresh the page after each search.

Finally, it doesn't handle large negative temperatures very well.


City:

Tuesday, February 16, 2016

w6d2 - DOM, bootstrap & javascript

For as much time as I spent learning javascript on my own, today was pretty disappointing.  (months!  The codeacademy track and the first 5 chapters of Eloquent Javascript were my mainstay.)  To be fair, it's been since September since I last did anything substantial with JS...but still.

To be even more fair, I did understand my goals with all the JS exercises (write for loops, while loops, and even a recursive function for factorial) I at least understood the challenge.  It was the syntax of js that had become unfamiliar after all this python.  Fortunately, I was able to pick it up again reasonably quickly.

Tonight, I started the CodeAcademy jQuery track afresh.  Hopefully that will give me a head start on tomorrow.

I wish I had more to add.  Bootstrap looks interesting since css is my least part of web development.

I'd like to write a more "meta" post on how this is all feeling...but it's always so late by the time I get to write that my more eloquent side is already dozing.  One day...

Monday, February 15, 2016

w6d1 - Planning the CatFinder

The cat finder project is moving ahead a bit:  I have user stories, a database scheme, some wireframes (that tail off into rudimentary...) and a Google Maps api key.  Woo.

Here's the super short Draw.io flow.  Each site user can register a sighting of a cat.  There may be many sightings that pertain to a single cat, so each cat "object" in Django will potentially have many sightings.  Ideally, each sighting will have just one user and just one cat.  The cat it refers to can change based on further information.  There is no independent verification of any particular cat's existence, so the first new sighting of a cat will be a new cat instance.  A sighting of an already existing cat will count as an additional review/sighting for that cat.





The sighting will be the primary data entry: user (foreign key), text description, rating, friendliness, distinguishing characteristics, location, timestamp of sighting.

Photos will go in a separate table since each sighting could include multiple photos/videos.  Each photo will have a foreign key to link it to a unique sighting.

When a user logs in and has found a cat:

they will be prompted to enter a location (using google maps geolocate or a physical address).  They will then be asked if their sighting refers to a cat already in the database (based on existing sightings within 0.5 miles):

If a previous sighting of this cat exists, they can add a new sighting linked to the existing cat.  (yeah, this one is pretty rudimentary but it has the essential information for populating a table, along with auto-generated time stamp and the geolocation info from the initial entry.)


If not, the new sighting will count as a new cat.  Each cat will have a page with all the pictures from the sightings that refer to the same cat (much like a restaurant can have multiple reviews on a review site.).


There will also be "flag this entry/photo" options throughout to allow users to report spam and inappropriate content (like pictures of dogs).  I'm hoping for a reddit-style moderation system with volunteer moderators for local areas and a few admins (i.e. me) who can appoint moderators and resolve bigger conflicts.  Moderators would be able to join sightings that were reported to be of unique cats, but are really of the same animal.  Or, the reverse (split sightings inappropriately tagged as the same animal).  Finally, if I'm able to impelement a "lost pet bulletin" a local moderator would have to approve an email sent out to site members who have opted in to the "Orange-Collar Alert"

Finally, users can create a profile for themselves, with photos, social media integration (of course!), and stats on how many cats and how many sightings they are responsible for.

Finally, finally, a user can search for cats near a specific location.  The location of any particular cat will be approximate (to a half mile or so) to avoid making it too easy to find friendly cats.  The exact location will be available to the person who uploaded it, but to no one else.

__

As I'm writing this up, I realize that I could use the same basic format for birders, or a catalog of plants in a city park (or crowd sourcing street tree data for a city).  A cat finder is inherently silly, and perilously close to the dreaded "facebook for ferrets".  It's contained, though, and of a scope I can imagine completing (as in it will have rudimentary functionality for limited users) in the next few weeks.  In the process, I'll learn to set up my own Django project with interconnected sql tables, I'll design some basic UI and forms, I'll apply some simple Javascript, and I'll learn some Google api mechanics.  There is a lot of room for science education and crowd-sourced science initiatives and I think this project will be a good ramp up to a larger project.




Sunday, February 14, 2016

w5d5 - Planning something big

Day 5 saw the implementation of some more elements of a simple web store- a checkout page and a user profile page.  These I did without input from the instructors.  They work ok, but it's nothing super exciting.  I suppose the good part is that I'm starting to 'get' basic django well enough that I don't need step-by-step instructions.

Friday afternoon, and much of today, was spent working on plans for a bigger, standalone project.  We're supposed to go through a complete cycle of project development, starting with "user stories" and wireframe mockups of pages.  I was divided between a teaching tool that an instructor could use to divide students by ability (set according to parameters the instructor chooses), and a light-hearted "Yelp for cats".  I went with FriendlyCatFinder as my project.  I really like the student working group project, but it's much more than two weeks' work at this point in my development. (The plan was to let a teacher upload a short quiz, then split students in to equal, random groups such that each smaller group has a mix of top, medium, and low-performing students.  Instructors with large classes face the problem that they don't know individual students well enough to make groups on their own.  The smaller groups would then be given a case study or other long-format problem to consider, and answer as a unit.

FriendlyCatFinder, by contrast, has a much more unified structure.  It would be a simple geolocation app with a form for rating cats encountered in public.  How pettable, how approachable, etc.  It's cute and fun, and I'll get a chance to learn the google maps api as well as build a site with minimal support.

Tomorrow: bootstrap, and a return (for me) to javascript.

Thursday, February 11, 2016

w5d4 - More Django, and a capstone project

Django is becoming a little less like magic, and a bit more like a really advanced technology.  To put it another way, I've been working pretty hard on pulling the pieces apart to see why things work.  I know there's some tons of stuff that's entirely beyond me at this point that happens underneath, but I'm at least starting to get a handle on the surface things.

We're still working with a super-simple web store.  Today we split the items with pagination, and added a shopping cart (of sorts - more a bin for holding items we select).  I figured out how to delete things from the cart (adding things was mapped out for us), and how to show a total based on the prices for the individual items.  I also figured out how to add links to all the pages to jump around more easily (not on the program at all, but it seems weird to have a site without internal links.

It all sounds so trivial when written out.  It didn't feel trivial going through it.  It didn't feel trivial, either, when I was going over the code this evening with a friend in the class.  Understanding the properties of the request object, the user object, and the shopping cart object, and how they interact with the product objects...it takes a bit of work.  Frequent use of dir(), command line sqlite3 to keep up with changes, and multiple print statements to monitor the status of the program all helped too.

It's dispiriting at times to think that I'm just 3 weeks from being done, with so much left to do.  Seriously, this stuff feels so minor and inconsequential...but I suppose everyone starts off small and works up.  Hopefully this is the steepest part of the curve....

Regardless, I need to be thinking of a capstone project.  I have a few ideas, but they seam either derivative, or too complex.  We'll see what happens.