My question is about points is javafx:
I have two points for example:
Point p1 = new Point(2,2);
Point p2 = new Point(10,8);
And i draw a imaginary line between them, i wanna know if i say x = 8 what y is on the imaginary line or otherwise is i know y, what x is on the imaginary line.
I found a formula
x= x1 + blend * (x2 - x1),
and blend is the percentage on the line.
But i wanna have this kind of formula where i can give a Y.
It there some kind of formula to solve this problem?
Thanks in advance!
Since your imaginary line is an equation in the format y = ax+b, you can solve it like this http://www.wikihow.com/Calculate-Slope-and-Intercepts-of-a-Line
given
10,8
and
2,2
y = ax + b
so
8 = 10a + b
-
2 = 2a + b
------------
6 = 8a
a = 6/8 = 0.75
and
8 = 10a + b
8 = 10*(0.75) + b
8 = 7.5 + b
8 - 7.5 = b
0.5 = b
so
y = 0.75*x + 0.5
Related
I have the following data-set, and need to plot a surface based on this set of data (of 60 3D points). Here, X, Y is the horizontal plane coordinates, and Z is the vertical / height coordinate.
p = read.csv("points.csv")
PTS X Y Z
1 101 481897.9 5456408 94.18695
2 102 481888.8 5456417 94.30702
3 103 481877.0 5456410 94.29034
4 104 481879.9 5456425 94.25546
5 105 481872.7 5456424 94.09370
After looking through several posts and trying to use functions in several libraries, I still cannot figure out a way to properly plot the surface. I've tried the following:
library(plotly)
plot_ly( y= Y, x = X, z = Z, data=p, type = "surface") #returns empty graphic frame
PX = data.matrix(p$X)
PY = data.matrix(p$Y)
PZ = data.matrix(p$Z)
library(plot3D)
surf3D(PX, PY, PZ)
#returns: Error in if (is.na(var)) ispresent <- FALSE else if (length(var) == 1) if (is.logical(var)) if (!var) ispresent <- FALSE :
argument is of length zero
library(lattice)
wireframe(p$Z ~ p$X*p$Y, data = p) #returns just a cube
library(rgl)
surface3d(p$X,p$Y,p$Z)
#returns: Error in rgl.surface(x = c(481897.916, 481888.8482, 481876.9524, 481879.9393, : y' length != 'x' rows * 'z' cols;
#although there are 60 data points in the form (X,Y,Z) in the data set, with no points missing any coordinate
I must have been doing something horribly wrong here. Would anyone mind to point out what the mistake is?
You cannot make a 3D surface plot with this data because to do it you have to have Z value for each (X,Y) couple, like this :
X1 X2 X3 ... Xn
Y1 Z11 Z12 Z13 ... Z1n
Y2 Z21 Z22 Z23 ... Z2n
Y3 Z31 Z32 Z33 ... Z3n
. .
. .
. .
Ym Zm1 Zm2 Zm3 ... Zmn
For example you don't have Z value for (481897.9,5456417) couple.
So, all you can do is a scatter3d plot :
plot_ly(data = p,x = X,y = Y, z = Z,type = "scatter3d",showlegend = FALSE)
In the following example, I want have zone on y axis, then plot D1 with its standard deviation (shading) D1sd on x axis. Next, I want to add D1b and its standard deviation on the second x axis. My second question is that, is it possible to plot the second set of data D2 in a panel next to first one. I'm thinking of the way spplot puts the panels next to each other. Thanks!
zone D1 D1sd D1b D1bsd D2 D2sd D2b D2bsd
-10 6.018198819 1.353674355 0.820238734 0.299921523 6.149905542 1.559112995 0.71903318 0.281436916
-9 6.016694189 1.348320178 0.790463895 0.320471326 6.225247218 1.810133214 0.690944285 0.291123921
-8 6.075920068 1.268199241 0.792396958 0.295767298 6.452827975 1.890055573 0.698130383 0.285354803
-7 6.014926533 1.15754388 0.826652396 0.269340472 6.364786271 1.677836628 0.748784125 0.262342978
-6 5.934024155 1.097224151 0.876312952 0.287715603 6.167672962 1.558124318 0.755995918 0.265152681
-5 6.180879693 1.115373166 0.911045374 0.302416557 6.429580579 1.485044161 0.783518016 0.255475422
-4 6.215761357 1.287465467 0.930981232 0.302896699 6.579955644 1.388358072 0.810873074 0.234479504
-3 6.191414137 1.297136068 0.859521028 0.301839757 6.72533907 1.383269712 0.786424272 0.242793151
-2 6.249558839 1.484243431 0.870789671 0.315339266 6.738830636 1.39348093 0.822833797 0.28853238
-1 6.279693424 1.462642241 0.890051094 0.313090388 6.665698185 1.272444414 0.849884276 0.309606843
0 6.389352438 1.653046732 0.911295197 0.332748249 6.623842834 1.3384852 0.860175975 0.311888845
1 6.421109477 1.954238381 0.917046385 0.349039084 6.633736605 1.627187751 0.880706612 0.346350393
2 6.187522396 1.994178951 0.881417644 0.38571426 6.422238767 1.685610306 0.875399565 0.351651773
3 5.975654953 2.180870669 0.871365681 0.444535385 6.245207747 1.925609129 0.915266481 0.424662193
4 5.681784682 2.182018258 0.846469896 0.38550673 6.004553419 1.947533306 0.890484046 0.404342645
5 5.550390285 2.189799132 0.834608476 0.340348644 5.831848009 1.849502381 0.887486532 0.387460845
6 5.382758749 2.460409982 0.832118248 0.360057614 5.810419947 2.06423957 0.954814407 0.38078381
7 4.819027419 2.643911373 0.78895866 0.38043413 5.42194855 2.259929373 0.935858628 0.37891625
8 3.782918423 2.584426217 0.643611576 0.335647266 4.418220284 2.186679796 0.790979174 0.364691895
9 3.064023314 2.528951519 0.496242154 0.294101493 3.64670387 2.091471213 0.592464821 0.341064247
10 2.62392179 2.707531426 0.380282732 0.249942178 3.159422995 2.392110771 0.452474888 0.334645666
Load in data
dat <- read.table(text = "zone D1 D1sd D1b D1bsd D2 D2sd D2b D2bsd
-10 6.018198819 1.353674355 0.820238734 0.299921523 6.149905542 1.559112995 0.71903318 0.281436916
-9 6.016694189 1.348320178 0.790463895 0.320471326 6.225247218 1.810133214 0.690944285 0.291123921
-8 6.075920068 1.268199241 0.792396958 0.295767298 6.452827975 1.890055573 0.698130383 0.285354803
-7 6.014926533 1.15754388 0.826652396 0.269340472 6.364786271 1.677836628 0.748784125 0.262342978
-6 5.934024155 1.097224151 0.876312952 0.287715603 6.167672962 1.558124318 0.755995918 0.265152681
-5 6.180879693 1.115373166 0.911045374 0.302416557 6.429580579 1.485044161 0.783518016 0.255475422
-4 6.215761357 1.287465467 0.930981232 0.302896699 6.579955644 1.388358072 0.810873074 0.234479504
-3 6.191414137 1.297136068 0.859521028 0.301839757 6.72533907 1.383269712 0.786424272 0.242793151
-2 6.249558839 1.484243431 0.870789671 0.315339266 6.738830636 1.39348093 0.822833797 0.28853238
-1 6.279693424 1.462642241 0.890051094 0.313090388 6.665698185 1.272444414 0.849884276 0.309606843
0 6.389352438 1.653046732 0.911295197 0.332748249 6.623842834 1.3384852 0.860175975 0.311888845
1 6.421109477 1.954238381 0.917046385 0.349039084 6.633736605 1.627187751 0.880706612 0.346350393
2 6.187522396 1.994178951 0.881417644 0.38571426 6.422238767 1.685610306 0.875399565 0.351651773
3 5.975654953 2.180870669 0.871365681 0.444535385 6.245207747 1.925609129 0.915266481 0.424662193
4 5.681784682 2.182018258 0.846469896 0.38550673 6.004553419 1.947533306 0.890484046 0.404342645
5 5.550390285 2.189799132 0.834608476 0.340348644 5.831848009 1.849502381 0.887486532 0.387460845
6 5.382758749 2.460409982 0.832118248 0.360057614 5.810419947 2.06423957 0.954814407 0.38078381
7 4.819027419 2.643911373 0.78895866 0.38043413 5.42194855 2.259929373 0.935858628 0.37891625
8 3.782918423 2.584426217 0.643611576 0.335647266 4.418220284 2.186679796 0.790979174 0.364691895
9 3.064023314 2.528951519 0.496242154 0.294101493 3.64670387 2.091471213 0.592464821 0.341064247
10 2.62392179 2.707531426 0.380282732 0.249942178 3.159422995 2.392110771 0.452474888 0.334645666", header = T)
First simple solution
A first attempt. This first way is the 'normal' way of doing this. Normally we could flip x and y with coord_flip(), but that doesn't work with facets and free scales, unfortunately.
library(ggplot2)
dat2 <- data.frame(D = rep(c("D1", "D1b", "D2", "D2b"), each = nrow(dat)),
group = rep(c('1', '2'), each = nrow(dat) * 2),
zone = dat$zone,
value = unlist(dat[c(2, 4, 6, 8)]),
SD = unlist(dat[c(3, 5, 7, 9)]))
ggplot(dat2, aes(zone, value, ymin = value - SD, ymax = value + SD, fill = group)) +
geom_point() + geom_line() + geom_ribbon(alpha = 0.2) +
facet_wrap(~D, scales = 'free') +
theme_bw()
A solution with flipped axes
You can actually get flipped axes when you manually draw the polygons. This code is hardly pretty, but you should get the idea.
polydat <- data.frame(D = rep(c("D1", "D1b", "D2", "D2b"), each = nrow(dat) * 2),
value = c(dat$D1 - dat$D1sd, rev(dat$D1 + dat$D1sd),
dat$D1b - dat$D1bsd, rev(dat$D1b + dat$D1bsd),
dat$D2 - dat$D2sd, rev(dat$D2 + dat$D2sd),
dat$D2b - dat$D2bsd, rev(dat$D2b + dat$D2bsd)),
zone = c(dat$zone, rev(dat$zone)),
group = rep(c('1', '2'), each = nrow(dat) * 4))
ggplot(dat2, aes(value, zone, fill = group)) +
geom_point() + geom_path() +
geom_polygon(data = polydat, alpha = 0.2) +
facet_wrap(~D, scales = 'free') +
theme_bw()
One way of getting this into two plots is to normalize the data into a common x-axis first (using scale for example).
Suppose I have following data for a student's score on a test.
set.seed(1)
df <- data.frame(question = 0:10,
resp = c(NA,sample(c("Correct","Incorrect"),10,replace=TRUE)),
score.after.resp=50)
for (i in 1:10) {
ifelse(df$resp[i+1] == "Correct",
df$score.after.resp[i+1] <- df$score.after.resp[i] + 5,
df$score.after.resp[i+1] <- df$score.after.resp[i] - 5)
}
df
.
question resp score.after.resp
1 0 <NA> 50
2 1 Correct 55
3 2 Correct 60
4 3 Incorrect 55
5 4 Incorrect 50
6 5 Correct 55
7 6 Incorrect 50
8 7 Incorrect 45
9 8 Incorrect 40
10 9 Incorrect 35
11 10 Correct 40
I want to get following graph:
library(ggplot2)
ggplot(df,aes(x = question, y = score.after.resp)) + geom_line() + geom_point()
My problem is: I want to color segments of this line according to student response. If correct (increasing) line segment will be green and if incorrect response (decreasing) line should be red.
I tried following code but did not work:
ggplot(df,aes(x = question, y = score.after.resp, color=factor(resp))) +
geom_line() + geom_point()
Any ideas?
I would probably approach this a little differently, and use geom_segment instead:
df1 <- as.data.frame(with(df,cbind(embed(score.after.resp,2),embed(question,2))))
colnames(df1) <- c('yend','y','xend','x')
df1$col <- ifelse(df1$y - df1$yend >= 0,'Decrease','Increase')
ggplot(df1) +
geom_segment(aes(x = x,y = y,xend = xend,yend = yend,colour = col)) +
geom_point(data = df,aes(x = question,y = score.after.resp))
A brief explanation:
I'm using embed to transform the x and y variables into starting and ending points for each line segment, and then simply adding a variable that indicates whether each segment went up or down. Then I used the previous data frame to add the original points themselves.
Alternatively, I suppose you could use geom_line something like this:
df$resp1 <- c(as.character(df$resp[-1]),NA)
ggplot(df,aes(x = question, y = score.after.resp, color=factor(resp1),group = 1)) +
geom_line() + geom_point(color = "black")
By default ggplot2 groups the data according to the aesthetics that are mapped to factors. You can override this default by setting group explicitly,
last_plot() + aes(group=NA)
I have a table with 3 columns
x y f
-101.0 -101.0 0.0172654144157
...
x and y are coordinates. f is value.
I want to make a 2d picture, where x and y are coordinates and f is a colour. But I need this picture to be not a number of coloured points, but a continuous schedule.
Help me someone please
There are a couple of simple ways to do this if you have a regular grid with your data. Try:
require(ggplot2)
require(lattice)
# make some data
s = 100
i = 0.5
x0 <- 27
y0 <- 34
df <- expand.grid(x=seq(0,s,i), y=seq(0,s,i))
df <- transform(df, f = cos( 10*pi * sqrt((x - x0)^2 + (y-y0)^2)))
# try as points
ggplot(df,aes(x,y,color=f)) + geom_point()
# or as tile
ggplot(df,aes(x,y,fill=f)) + geom_tile()
# or even easier with lattice
levelplot(f ~ x * y, df)
Output examples:
I have a line that exists in 3D that is between two known points: {X1, Y1, Z1} and {X2, Y2, Z2}.
(X1,Y1,X1)----------(X2,Y2,Z2)
There is a point (Xd,Yd,Zd) on the line between those points at distance D from (X1,Y1,Z1).
(X1,Y1,X1)---D---(Xd,Yd,Zd)-----(X2,Y2,Z2)
How can I determine the coordinates of point (Xd,Yd,Zd)?
Assuming you want to move the distance D from point 1 to point 2 :
P1 = [ X1, Y1, Z1 ]
P2 = [ X2, Y2, Z2 ]
The line vector can be described as :
V = P2 - P1 = [ Xv = X2 - X1, Yv = Y2 - Y1, Zv = Z2 - Z1 ]
The line's length can be determined as :
VL = SQRT(Xv^2 + Yv^2 + Zv^2) // ^2 = squared
The line's versor aka the unit vector can be determined as :
v = V / VL = [Xv / VL, Yv / VL, Zv / VL]
The target point PD can be determined as :
Pd = P1 + D * v // Starting from P1 advance D times v
Please note that P1 and v are vectors and D is a scalar
First, determine the length of the line segment:
d=sqrt((X1-X2)^2+(Y1-Y2)^2+(Z1-Z2)^2))
You are moving D from P1=(X1,Y1,Z1) toward P2=(X2,Y2,Z2). This puts you at the point (X3,Y3,Z3):
{XYZ}3={XYZ}1+(D/d)*({XYZ}2-{XYZ}1})
Where you expand that into 3 equations, one for each of X, Y, and Z.
This works because you are D/d of the way between P1 and P2. Check: Say D=d. Then you should be at exactly P2.
Take the vector between the two points
<X2-X1, Y2-Y1, Z2-Z1>
Turn that into a unit vector pointing in the same direction but with length 1. You do that by dividing by the distance between the two points:
<X2-X1, Y2-Y1, Z2-Z1>
---------------------------------------
sqrt((X2-X1)^2 + (Y2-Y1)^2 + (Z2-Z1)^2)
Then multiply that by D and add to your original point to get the new point.
<X2-X1, Y2-Y1, Z2-Z1>
(X1, Y1, Z1) + D * ---------------------------------------
sqrt((X2-X1)^2 + (Y2-Y1)^2 + (Z2-Z1)^2)
This is a linear combination problem:
dist = distance(p1, p2)
distance D is given
f = D / dist (fractional coordinate of point D within LineSeg (p1, p2)
pD = LinearCombo (1-f, p1, f, p2) (coordinates of point distance D from p1)