我想创建点,然后将它们存储在数组中。我这样做是为了对数据点进行线性回归。所以我需要能够遍历我所有的观点。
我无法在网络上找到类似的东西进行处理,而且由于我实际上做不到,因此需要您的帮助。这是我的方法,但似乎不起作用:
ArrayList<dataPoint> dataPoints = new ArrayList<dataPoint>();
void setup(){
size(1000, 1000);
background(255);
}
void draw(){
for (int i = 1; i == dataPoints.size(); i++) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
dataPoint Point = dataPoints.get(i);
Point.display();
}
}
void mousePressed() {
dataPoints.add(new dataPoint(mouseX, mouseY));
}
class dataPoint {
float x;
float y;
dataPoint(int tempX, int tempY) {
x = tempX;
y = tempY;
}
void display() {
strokeWeight(10);
stroke(255,0,0);
point(x,y);
}
}
我想要一个程序来创建点并将其存储在数组中(或类似的东西,您可以循环遍历)。
答案 0 :(得分:1)
您的大多数代码都有意义,我只能发现两个陷阱,它们可能会阻止您遍历所有点并将其可视化:
for (int i = 0; i < dataPoints.size(); i++)
在Processing / Java中,请记住数组索引从0开始(同样,最后一个索引将不是数组的size()
,而是数组的<
,因此少了1,因此,for条件中的ArrayList<dataPoint> dataPoints = new ArrayList<dataPoint>();
void setup(){
size(1000, 1000);
}
void draw(){
background(255);
for (int i = 0; i < dataPoints.size(); i++) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
dataPoint Point = dataPoints.get(i);
Point.display();
}
}
void mousePressed() {
dataPoints.add(new dataPoint(mouseX, mouseY));
}
class dataPoint {
float x;
float y;
dataPoint(int tempX, int tempY) {
x = tempX;
y = tempY;
}
void display() {
strokeWeight(10);
stroke(255,0,0);
point(x,y);
}
}
) / p>
这是经过上述调整的代码:
<script src="<?php echo base_url(); ?>assets/fullcalendar/lib/moment.min.js"></script>
<script src="<?php echo base_url(); ?>assets/fullcalendar/lib/jquery-ui.min.js"></script>
<script src="<?php echo base_url(); ?>assets/fullcalendar/lib/jquery.min.js"></script>
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/fullcalendar/fullcalendar.min.css" />
<script src="<?php echo base_url(); ?>assets/fullcalendar/fullcalendar.min.js"></script>
<script src="<?php echo base_url(); ?>assets/fullcalendar-3.9.0/locale/es.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Calendar</h1>
<div id="calendar">
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Calendar Event</h4>
</div>
<div class="modal-body">
<?php echo form_open(site_url('calendar/add_event'), array("class" => "form-horizontal")) ?>
<div class="form-group">
<label for="p-in" class="col-md-4 label-heading">Event Name</label>
<div class="col-md-8 ui-front">
<input type="text" class="form-control" name="name" value="">
</div>
</div>
<div class="form-group">
<label for="p-in" class="col-md-4 label-heading">Description</label>
<div class="col-md-8 ui-front">
<input type="text" class="form-control" name="description">
</div>
</div>
<div class="form-group">
<label for="p-in" class="col-md-4 label-heading">Start Date</label>
<div class="col-md-8">
<input type="text" class="form-control" name="start_date">
</div>
</div>
<div class="form-group">
<label for="p-in" class="col-md-4 label-heading">End Date</label>
<div class="col-md-8">
<input type="text" class="form-control" name="end_date">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Add Event">
<?php echo form_close() ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>-->
<script type="text/javascript">
$(document).ready(function() {
var date_last_clicked + null;
$('#calendar').fullCalendar({
eventSources: [
{
events: function(start, end, timezone, callback) {
$.ajax({
url: '<?php echo base_url() ?>calendar/get_events',
dataType: 'json',
data: {
// our hypothetical feed requires UNIX timestamps
start: start.unix(),
end: end.unix()
},
success: function(msg) {
var events = msg.events;
callback(events);
}
});
}
},
]
dayClick: function(date, jsEvent, view) {
date_last_clicked = $(this);
$(this).css('background-color', '#bed7f3');
$('#addModal').modal();
},
});
});
</script>
<style>
#calendar{
width: 800px;
margin: 0px auto;
}
</style>
</body>