Skip to content

Instantly share code, notes, and snippets.

@henryscala
Created August 31, 2014 12:52
Show Gist options
  • Save henryscala/36cd2c68542fd0bddb0a to your computer and use it in GitHub Desktop.
Save henryscala/36cd2c68542fd0bddb0a to your computer and use it in GitHub Desktop.
cyclelistAndCanvasOfDart
import 'dart:html';
void main() {
Vec2 p1=new Vec2(50,50);
Vec2 p2=new Vec2(100,50);
Vec2 p3=new Vec2(100,100);
Vec2 p4=new Vec2(50,100);
HtmlCanvas canvas = new HtmlCanvas("#canvas")
canvas.context.beginPath();
canvas.context.moveTo(p1.x, p1.y);
canvas.context.lineTo(p2.x, p2.y);
canvas.context.lineTo(p3.x, p3.y);
canvas.context.lineTo(p4.x, p4.y);
canvas.context.lineWidth=4;
canvas.context.strokeStyle='red';
canvas.context.closePath();
canvas.context.stroke();
}
class HtmlCanvas{
CanvasRenderingContext2D _context = null;
String _id;
HtmlCanvas (String id) {
_id = id;
_context = (querySelector(id) as CanvasElement).context2D;
}
CanvasRenderingContext2D get context {
return _context;
}
String toString(){
return "HtmlCanas($_id)";
}
}
class Vec2 {
var vec=<num>[0,0];
num get x {
return vec[0];
}
set x(num value){
vec[0]=value;
}
num get y {
return vec[1];
}
set y(num value){
vec[1] = value;
}
Vec2 (num x, num y){
vec[0]=x;
vec[1]=y;
}
Vec2.fromList (List<num> a){
assert(a.length == 2);
vec[0] = a[0];
vec[1] = a[1];
}
String toString(){
return "(${vec[0]},${vec[1]})";
}
}
class ListNode<T>{
T value;
ListNode<T> prev=null;
ListNode<T> next=null;
ListNode(T v,[ListNode<T> p,ListNode<T> n]){
value = v;
prev = p;
next = n;
}
String toString(){
return "Node($value)";
}
}
class CycleList<T>{
ListNode<T> head=null;
int _size = 0;
int get size {
return _size;
}
void append(T v){
ListNode<T> node = new ListNode<T>(v);
if(head == null){
head = node;
node.prev=head;
node.next=head;
} else {
node.prev = head.prev;
node.next = head;
head.prev = node;
node.prev.next = node;
}
_size ++;
}
void remove(ListNode<T> node){
assert(head != null);
assert(size > 0);
if(size==1){
head = null;
} else {
node.prev.next = node.next;
node.next.prev = node.prev;
}
_size --;
}
String toString(){
StringBuffer sb=new StringBuffer();
sb.write("[");
var node = head;
do {
if(node ==null) break;
sb.write(node);
node=node.next;
} while(node!=head);
sb.write("]");
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment