Skip to content

Instantly share code, notes, and snippets.

@weitsai
Last active December 21, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weitsai/6273348 to your computer and use it in GitHub Desktop.
Save weitsai/6273348 to your computer and use it in GitHub Desktop.
台北市判斷
public class NewTaipeiCity {
public static void main(String[] args) {
// 新北
getCity(25.054501, 121.76686);
getCity(25.025884, 121.680343);
getCity(25.040193, 121.685587);
// 臺北
getCity(25.029306, 121.5774);
getCity(25.160823, 121.519017);
getCity(25.06041,121.407663);
}
public static void getCity(double x, double y) {
double tmp = x;
x = y;
y = tmp;
int right_node = 0, left_node = 0;
double lasty = 0.0, lastx = 0.0;
for (int i = 0; i < newTaipeiCity.length; i++) {
if (lastx == 0.0) {
lastx = newTaipeiCity[i][0];
lasty = newTaipeiCity[i][1];
continue;
}
if ((newTaipeiCity[i][1] >= y && y >= lasty) || (lasty >= y && y >= newTaipeiCity[i][1])) {
if (x >= newTaipeiCity[i][0] && x >= lastx) {
right_node++;
} else if (x <= newTaipeiCity[i][0] && x <= lastx) {
left_node++;
} else {
double deltax, deltay, tempx;
deltax = newTaipeiCity[i][0] - lastx;
deltay = newTaipeiCity[i][1] - lasty;
tempx = (y - lasty) * deltax / deltay + lastx;
if (x >= tempx) {
right_node ++;
} else {
left_node ++;
}
}
}
lastx = newTaipeiCity[i][0];
lasty = newTaipeiCity[i][1];
}
if (left_node % 2 == 1 && right_node % 2 == 1) {
System.out.println("新北市");
} else {
System.out.println("你不在臺北市內喔!");
}
}
static double newTaipeiCity[][] = {
{
121.53733049483, 25.300032002093
}, {
121.61756668038, 25.277590953571
}, {
121.67121072118, 25.202490045613
}, {
121.70193318825, 25.216994847091
}, {
121.68523030612, 25.190709072299
}, {
121.70100571974, 25.169279467232
}, {
121.62678349157, 25.139961967398
}, {
121.68718729003, 25.072643046038
}, {
121.73024094348, 25.052998718406
}, {
121.76458143015, 25.0577133542
}, {
121.77576897684, 25.070659770513
}, {
121.74990722033, 25.109867648173
}, {
121.79490982715, 25.112794380976
}, {
121.80369893442, 25.136033401195
}, {
121.82663070725, 25.135986758174
}, {
121.82165024699, 25.121089533354
}, {
121.9238931126, 25.129853544104
}, {
121.91294874653, 25.08834968138
}, {
121.93210680178, 25.032670579472
}, {
122.00668776227, 25.008523116287
}, {
121.96674054519, 24.982445759566
}, {
121.84691725985, 24.960294950818
}, {
121.83903420322, 24.944685345687
}, {
121.85392133245, 24.925716847405
}, {
121.83598719835, 24.907932051587
}, {
121.79198538152, 24.899868736245
}, {
121.77545352397, 24.865109809513
}, {
121.73051587064, 24.855299419709
}, {
121.68822603565, 24.816108129338
}, {
121.62579713219, 24.80878057732
}, {
121.62542846149, 24.795059147544
}, {
121.5921920992, 24.787804673827
}, {
121.59739932321, 24.727528748808
}, {
121.5432536323, 24.705662351765
}, {
121.53678844597, 24.688425406252
}, {
121.47984504689, 24.673775249566
}, {
121.42327895575, 24.761894771716
}, {
121.44949569593, 24.786030515329
}, {
121.43653488399, 24.812032448597
}, {
121.40294695301, 24.843459917096
}, {
121.37354379403, 24.83326643682
}, {
121.33606688552, 24.843828417193
}, {
121.35801530161, 24.85321218098
}, {
121.34502965278, 24.866932114319
}, {
121.35434957865, 24.873636470315
}, {
121.32029962734, 24.900198287167
}, {
121.31753431295, 24.91633795566
}, {
121.33975553019, 24.92848631809
}, {
121.31575482526, 24.938465783406
}, {
121.33158415025, 24.945627003873
}, {
121.32352450012, 24.972738766754
}, {
121.36931304469, 24.976886808486
}, {
121.40292361007, 24.998986561571
}, {
121.39115106845, 25.012072815541
}, {
121.41212958358, 25.019096532117
}, {
121.39063664406, 25.041858246687
}, {
121.40066182266, 25.051354027735
}, {
121.28264733005, 25.109904847269
}, {
121.29663439865, 25.126137246008
}, {
121.38949907793, 25.147027702212
}, {
121.45267941547, 25.252812950363
}, {
121.53733049483, 25.300032002093
}, {
121.55483607825, 25.208318016063
}, {
121.52027056401, 25.169928338343
}, {
121.50263545144, 25.172887118614
}, {
121.45705318542, 25.109224675278
}, {
121.50574281814, 25.075695605875
}, {
121.50487738207, 25.049645165412
}, {
121.48367012452, 25.036907841479
}, {
121.49057100185, 25.010975842478
}, {
121.51903399844, 25.01978590235
}, {
121.5604819403, 24.964987416783
}, {
121.59868336218, 24.960450281453
}, {
121.61998828188, 24.969445463606
}, {
121.60003963643, 24.97693546907
}, {
121.5975253634, 25.014585448118
}, {
121.66588221885, 25.029841896548
}, {
121.6191712559, 25.043510929108
}, {
121.62589875435, 25.096677522234
}, {
121.59734405832, 25.109622792222
}, {
121.60812344124, 25.131321171906
}, {
121.57698439417, 25.167138958642
}, {
121.5837912149, 25.194524776809
}, {
121.55483607825, 25.208318016063
}
};
}
@ronnywang
Copy link

54 行的:

                if (x >= (kaohsiungCoordinatesArr[i][0] - lastx)) {
                    right_node++;
                } else {
                    left_node++;
                }

應該改成:

            double deltax, deltay, tempx;
            deltax = kaohsiungCoordinatesArr[i][0] - lastx;
            deltay = kaohsiungCoordinatesArr[i][1] - lasty;
            tempx = (y - lasty) * deltax / deltay + lastx;
            if (x >= deltax) {
                right_node ++;
            } else {
                left_node ++;
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment