Skip to content

Instantly share code, notes, and snippets.

View StuartFarmer's full-sized avatar
🏹
take the shot

Stuart Farmer StuartFarmer

🏹
take the shot
  • Lisbon, Portugal
View GitHub Profile
@StuartFarmer
StuartFarmer / Overlaying MKMapView with OSM Data
Last active August 23, 2022 11:59
Quick snippet to overlay Apple map data with OSM data, MapBox data, or any other map that has an online tile system on the MKMapView
// Import these Frameworks:
#import <MapKit/MapKit.h>
// Include this delegate
@interface ViewController () <MKMapViewDelegate>
- (void)viewDidLoad
{
// Add this code to your preexisting viewDidLoad method
@StuartFarmer
StuartFarmer / Pull-up to Refresh View
Created July 24, 2014 00:42
Pull-up to Refresh View in Obj-C
// Put this in viewDidLoad where _webView is your targetted view
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[_webView.scrollView addSubview:refreshControl];
// Declare this method elsewhere in the implementation file
-(void)handleRefresh:(UIRefreshControl *)refresh {
// Code here
[refresh endRefreshing];
}
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
@StuartFarmer
StuartFarmer / Toggle Fades in a UIView Object
Last active August 29, 2015 14:04
Basic IBAction code to toggle fades in and out with animation for controls in UIView
- (IBAction)showToolbarButtonPressed:(id)sender {
// Set up conditional to setup toggle feature.
// This conditional checks if the toolbar is invisible, and if it is, it animates the opacity to 100%, otherwise, it assumes the toolbar is visible, and animates the opacity to 0%. Code can check any other conditionals, ie. self.toolbar.isHidden == YES, etc.
if (self.toolbar.alpha == 0.0f) {
// Disable button
[self.showToolbarButton setEnabled:NO];
// Fade toolbar in w/ Animation
[UIView animateWithDuration:2.0f
@StuartFarmer
StuartFarmer / Detect if MKAnnotation is in MKPolygon
Created August 4, 2014 18:10
The following converts the coordinate to a CGPoint in the polygon view and uses CGPathContainsPoint to test if that point is in the path (which may be non-rectangular)
CLLocationCoordinate2D mapCoordinate = ...; //user location or annot coord
MKMapPoint mapPoint = MKMapPointForCoordinate(mapCoordinate);
MKPolygonView *polygonView =
(MKPolygonView *)[mapView viewForOverlay:polygonOverlay];
CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
BOOL mapCoordinateIsInPolygon =
@StuartFarmer
StuartFarmer / setuplocationmanager
Last active August 29, 2015 14:20
Set up location manager
// Initialize location services.
locationManager = [[CLLocationManager alloc] init];
[locationManager requestAlwaysAuthorization];
locationManager.delegate = self;
// Accuracy constants here. These should be tested for battery optimization.
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
// Set up the CV Video Camera object to bring in video to the program
videoCamera = [[CvVideoCamera alloc] initWithParentView:self.imageView];
videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetHigh;
videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
videoCamera.defaultFPS = 30;
videoCamera.grayscaleMode = NO;
videoCamera.delegate = self;
# I'll refactor this later, but it's almost midnight and I just wanted to demonstrate how sick NEAT is at solving problems without defining any direction for the algoritm
from __future__ import print_function
import gym
import numpy as np
import itertools
import os
from neat import nn, population, statistics
@StuartFarmer
StuartFarmer / MountainCar with NEAT
Last active August 11, 2016 22:52
MountainCar with NEAT
from __future__ import print_function
import gym
import numpy as np
import itertools
import os
from neat import nn, population, statistics
# another great example of how crazy effective NEAT can be even when it knows literally nothing about what's going on in the environment.
@StuartFarmer
StuartFarmer / MountainCar config
Last active August 8, 2016 00:04
MountainCar config
#--- parameters for the XOR-2 experiment ---#
# The `Types` section specifies which classes should be used for various
# tasks in the NEAT algorithm. If you use a non-default class here, you
# must register it with your Config instance before loading the config file.
[Types]
stagnation_type = DefaultStagnation
reproduction_type = DefaultReproduction
[phenotype]