想了半天还是只能用grid.polygon来模拟半圆的点,然后连起来得半圆

不知道有没更好的办法

或者能不能画个圆,然后每个半圆显示不同颜色。

因为要在grid中画多个,所以不能用pie()这类函数
直接
<br />
par(pty = 's')<br />
plot(,type = 'l',xlim = ,ylim = )
theta=seq(0,2*pi,length.out=1000)

x=cos(theta)

y=sin(theta)

plot(x,y,type="l")

作一个完整的圆的code,半圆只要适当限制参数范围即可
<br />
?symbol<br />
<br />
install.packages('plotrix')<br />
?draw.circle<br />




这两个都能画圆,不知能不能画半圆。

要不先画个圆,再用个白色的正方形遮住一半……

[s:12]
看了一下 pie 的实现,它用的也是 polygon。既然要画多个,那就自己封装一个函数好了:


<br />
draw.pie <- function(x, y, r, init.angle = 0, angle = 180, ...) {<br />
    t <- seq(from = init.angle, to = init.angle + angle, by = sign(angle)) * pi / 180<br />
    polygon(c(x, x + r * cos(t)), c(y, y + r * sin(t)), ...)<br />
}<br />
<br />
plot(c(-1, 1), c(-1, 1), type = "n")<br />
draw.pie(0, 0, 0.6, col = "blue")<br />
draw.pie(0, 0, 0.5, 90, 210, col = "red")<br />
draw.pie(0, 0, 0.4, 135, 180, col = "yellow", border = FALSE)<br />
draw.pie(0, 0, 0.3, 180, 90, col = "green", border = FALSE)<br />
[quote]引用第2楼dclong于2009-07-28 23:26发表的  :

theta=seq(0,2*pi,length.out=1000)

x=cos(theta)

y=sin(theta)

plot(x,y,type="l")

作一个完整的圆的code,半圆只要适当限制参数范围即可

[/quote]



theta=seq(0,2*pi,length.out=1000)

x=cos(theta)

y=sin(theta)

plot(x,abs(y),type="l") 作一个半圆
[quote]引用第4楼yanlinlin82于2009-07-31 00:19发表的  :

看了一下 pie 的实现,它用的也是 polygon。既然要画多个,那就自己封装一个函数好了:


<br />
draw.pie <- function(x, y, r, init.angle = 0, angle = 180, ...) {<br />
    t <- seq(from = init.angle, to = init.angle + angle, by = sign(angle)) * pi / 180<br />
....... [url=http://cos.name/bbs/job.php?action=topost&tid=15951&pid=75104][/url]<br />
[/quote]<br />
谢谢
12 天 后
再问一个问题

不管是用grid.polygon()还是polygon()模拟出来的半圆

在图形拉动过程中都会由于长宽不一致而是半圆变成椭圆

谁能帮我想想怎么样才能避免这样么
要保持图形坐标的横纵比,在画图时加上参数asp=1。
7 天 后
可以用

draw.arc (in plotrix)

这个是根据圆心、半径和角度来画圆弧。



我自己写了一个画圆弧函数,

输入是两个端点,半径,自动计算出圆心,

还可以指定顺时针、逆时针,大弧还是小弧 -



my.angle <- function(x, y) {

    if (y >= 0)

        angle = acos(x)

    else

        angle = 2*pi - acos(x)

    angle

}

my.arc <- function(p0, p1, radius,

        small.arc = TRUE, clock.wise = FALSE, n = 35) {

    x0 = p0[1]

    x1 = p1[1]

    y0 = p0[2]

    y1 = p1[2]

    dx = x1 - x0

    dy = y1 - y0

    t = sqrt((radius^2 - (dx^2/4 + dy^2/4)) / (dx^2 + dy^2))

    if (xor(small.arc, clock.wise)) t = -t

    dcx = dx/2 + dy*t

    dcy = dy/2 - dx*t

    angle0 = my.angle(-dcx / radius, -dcy / radius)

    angle1 = my.angle((dx - dcx) / radius, (dy - dcy) / radius)

    if (!clock.wise) {

        if (angle1 < angle0) angle1 = angle1 + 2*pi

        angle = seq(angle0, angle1, length.out = n)

    }

    else {

        if (angle0 < angle1) angle0 = angle0 + 2*pi

        angle = seq(angle0, angle1, length.out = n)

    }

    cbind(x0 + dcx + radius * cos(angle), y0 + dcy + radius * sin(angle))

}



plot(my.arc(0, 1, 1, 0, small.arc = TRUE, clock.wise = TRUE)