#
#
# E. Korsching 2016





circle <- function(x, y, rx=0.5, ry=NULL, vis.true=T, col=10, angle=45, density=5)
{
	#add a circle (rx only) or ellipsoid (rx and ry) to a plot
	#if vis.true=T : plots a circle which is round in shape on the plot area irrespective
	# of the coordinates - good for illustration - bad for accuracy
	# x orientated
	#x,y: center of the circle, rx,ry: radius or main ellipse axes, col: color 1-16, angle: shading angle,
	# density=-1 : filled, =5: shaded, =0 : hollow, new=F: plot area
	
	if(is.null(ry)) ry <- rx
	
	if(vis.true){
		rel.size.plot.area <- par("plt")		#xmin, xmax, ymin, ymax , plot area system
		xy.plot.area <- par("usr")				#xmin, xmax, ymin, ymax , coordinate system
		
		#correction
		a1 <- (xy.plot.area[2]-xy.plot.area[1])		#dx
		a2 <- (xy.plot.area[4]-xy.plot.area[3])		#dy
		
		b1 <- (rel.size.plot.area[2]-rel.size.plot.area[1])		#dxpage
		b2 <- (rel.size.plot.area[4]-rel.size.plot.area[3])		#dypage
		
		if(a2>a1){			#y>x					correction + scaling
#			if(b2<b1){
				rx <- rx
				ry <- ry * (a2/a1)
#				ry <- ry * ((a2/a1)*(b2/b1))
#			}
#			if(b2>b1){
#				rx <- rx * (b1/b2)
#				ry <- ry * (a2/a1)
#			}
		}
		if(a2<a1){		#y<x
#			if(b2<b1){
				rx <- rx * (a1/a2)
				ry <- ry
#				ry <- ry * (b2/b1)
#			}
#			if(b2>b1){
#				rx <- rx * ((a1/a2)*(b1/b2))
#				ry <- ry
#			}
		}
	}
	
	z <- seq(0, 2 * pi, length=1000)
	xpos <- rx * cos(z)
	ypos <- ry * sin(z)
	polygon(x + xpos, y + ypos, density=density, angle=angle, border=T, col=col)
}

#plot(c(0,2), c(0,10))
#circle(x=1.5, y=1.5, rx=0.5, vis.true=T, col=2, density=-1)
#plot(c(0,10), c(0,2))
#circle(x=1.5, y=1.5, rx=0.5, vis.true=F, col=2, density=-1)

